Home>Article>Backend Development> How does PHP determine whether a website is being accessed via a mobile phone or a computer?

How does PHP determine whether a website is being accessed via a mobile phone or a computer?

coldplay.xixi
coldplay.xixi Original
2020-09-07 09:33:34 2511browse

How to determine whether the website is accessed by mobile phone or computer: first open the terminal editor; then enter the judgment code [CheckSubstrs($mobile_token_list,$useragent)]; and finally output the result.

How does PHP determine whether a website is being accessed via a mobile phone or a computer?

Related learning recommendations:php programming(Video)】

php method to determine whether the website is accessed by mobile phone or computer:

Method 1:

 0) { // Check whether the browser/gateway says it accepts WML. $br = "WML"; } else { $browser = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : ''; if(empty($browser)) return true; $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,$browser) || checkSubstrs($mobile_token_list,$browser); if($found_mobile) $br ="WML"; else $br = "WWW"; } if($br == "WML") { return true; } else { return false; } } function checkSubstrs($list,$str){ $flag = false; for($i=0;$i 0){ $flag = true; break; } } return $flag; } if(check_wap()){ echo "wap"; }else{ echo "web"; } ?>

Method 2:

0?$matches[0]:''; function CheckSubstrs($substrs,$text){ foreach($substrs as $substr) if(false!==strpos($text,$substr)){ return true; } return false; } $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; } } if (isMobile()) echo '手机登录'; else echo '电脑登录'; ?>

If you want to know more about programming learning, please pay attention to thephp trainingcolumn!

The above is the detailed content of How does PHP determine whether a website is being accessed via a mobile phone or a computer?. For more information, please follow other related articles on the PHP Chinese website!

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