現在通過手機訪問網站越來越流行了,如果我們希望統計一下網站通過pc,手機移動端的各自訪問量的情況,或者需要為手機移動端做一些特別的處理的話,那麼我們就需要對訪問網站的用戶的客戶端做一下鑒別了,下面這個實例就是通過php識別用戶是電腦還是手機訪問網站的方法。
<?php function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:''; $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ'); $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod'); $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock)||CheckSubstrs($mobile_token_list,$useragent); if($found_mobile){ return true; }else{ return false; } } function CheckSubstrs($substrs,$text){ foreach($substrs as $substr){ if(false!==strpos($text,$substr)){ return true; } return false; } } if(isMobile()){ echo '手機登錄 m.phpernote.com'; }else{ echo '電腦登錄 www.phpernote.com'; }