这是方法到目前为止我觉得判断的比较完善的,暂时记录下来使用。
/**
* 当前用户是否是移动端
* [@return](https://my.oschina.net/u/556800) boolean
*/
function isMobile()
{
//如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
{
return true;
}
//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset ($_SERVER['HTTP_VIA']))
{
// 找不到为flase,否则为true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
//判断手机发送的客户端标志,兼容性有待提高
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'
);
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
{
return true;
}
}
return false;
}