這是方法到目前爲止我以爲判斷的比較完善的,暫時記錄下來使用。css
/** * 當前用戶是不是移動端 * [@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; }