早睡早起,方能养生
Sleep early rise early, way to keep healthy

PHP判断是否是手机端

super
2020-11-07 18:41
views 2490

private function isMobile()
{
   if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array ('nokia', 'sony','ericsson','mot',
            'samsung','htc','sgh','lg','sharp',
            'sie-','philips','panasonic','alcatel',
            'lenovo','iphone','ipod','blackberry',
            'meizu','android','netfront','symbian',
            'ucweb','windowsce','palm','operamini',
            'operamobi','openwave','nexusone','cldc',
            'midp','wap','mobile'
        );
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){
          return true;
        } else {
          return false;
        }
    } else {
        return false;
    }
}

// 示例
public function test()
{
    if (self::isMobile()) {
        // 是手机端
        // ... code ...
    } else {
        // 不是手机端
        // ... code ...
    }
}




分享
0 条讨论
top