PHP判断PC端还是手机端

来源:互联网 发布:性能数据 守望先锋 编辑:程序博客网 时间:2024/05/16 14:04
  1. <?php  
  2. function isMobile(){  
  3.     $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';  
  4.     $useragent_commentsblock=preg_match('|.?|',$useragent,$matches)>0?$matches[0]:'';  
  5.     function CheckSubstrs($substrs,$text){  
  6.         foreach($substrs as $substr){  
  7.             if(false!==strpos($text,$substr)){  
  8.                 return true;  
  9.             }else{  
  10.                 return false;  
  11.             }  
  12.         }  
  13.     }  
  14.     $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');  
  15.     $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');  
  16.        
  17.     $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) || CheckSubstrs($mobile_token_list,$useragent);  
  18.        
  19.     if ($found_mobile){  
  20.         return true;  
  21.     }else{    
  22.         return false;  
  23.     }  
  24. }  
  25. if (isMobile())  
  26. echo 'phone';  
  27. else  
  28. echo 'pc';  
  29. ?> 
0 0
原创粉丝点击