Home >php教程 >php手册 >PHP识别电脑还是手机访问网站

PHP识别电脑还是手机访问网站

WBOY
WBOYOriginal
2016-06-07 11:43:251237browse

最近,客户要求网站要判断是手机访问还是电脑访问,然后显示不同的信息,所以就在网上找了,这个,不是原创,重在分享。
<?php <br /> function isMobile(){  <br>     $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';  <br>     $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';        <br>     function CheckSubstrs($substrs,$text){  <br>         foreach($substrs as $substr)  <br>             if(false!==strpos($text,$substr)){  <br>                 return true;  <br>             }  <br>             return false;  <br>     }<br>     $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');<br>     $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');  <br>           <br>     $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||  <br>               CheckSubstrs($mobile_token_list,$useragent);  <br>           <br>     if ($found_mobile){  <br>         return true;  <br>     }else{  <br>         return false;  <br>     }  <br> }<br> if (isMobile())<br>     echo '手机登录m.php100.com';<br> else<br>     echo '电脑登录www.php100.com';<br> ?>

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn