Home >Backend Development >PHP Problem >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.xixiOriginal
2020-09-07 09:33:342611browse

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:

<?php 
function check_wap() {  
    if (isset($_SERVER[&#39;HTTP_VIA&#39;])) return true;  
    if (isset($_SERVER[&#39;HTTP_X_NOKIA_CONNECTION_MODE&#39;])) return true;  
    if (isset($_SERVER[&#39;HTTP_X_UP_CALLING_LINE_ID&#39;])) return true;  
    if (strpos(strtoupper($_SERVER[&#39;HTTP_ACCEPT&#39;]),"VND.WAP.WML") > 0) {  
        // Check whether the browser/gateway says it accepts WML.  
        $br = "WML";  
    } else {  
        $browser = isset($_SERVER[&#39;HTTP_USER_AGENT&#39;]) ? trim($_SERVER[&#39;HTTP_USER_AGENT&#39;]) : &#39;&#39;;  
        if(empty($browser)) return true;
        $mobile_os_list=array(&#39;Google Wireless Transcoder&#39;,&#39;Windows CE&#39;,&#39;WindowsCE&#39;,&#39;Symbian&#39;,&#39;Android&#39;,&#39;armv6l&#39;,&#39;armv5&#39;,&#39;Mobile&#39;,&#39;CentOS&#39;,&#39;mowser&#39;,&#39;AvantGo&#39;,&#39;Opera Mobi&#39;,&#39;J2ME/MIDP&#39;,&#39;Smartphone&#39;,&#39;Go.Web&#39;,&#39;Palm&#39;,&#39;iPAQ&#39;);  
                    
        $mobile_token_list=array(&#39;Profile/MIDP&#39;,&#39;Configuration/CLDC-&#39;,&#39;160×160&#39;,&#39;176×220&#39;,&#39;240×240&#39;,&#39;240×320&#39;,&#39;320×240&#39;,&#39;UP.Browser&#39;,&#39;UP.Link&#39;,&#39;SymbianOS&#39;,&#39;PalmOS&#39;,&#39;PocketPC&#39;,&#39;SonyEricsson&#39;,&#39;Nokia&#39;,&#39;BlackBerry&#39;,&#39;Vodafone&#39;,&#39;BenQ&#39;,&#39;Novarra-Vision&#39;,&#39;Iris&#39;,&#39;NetFront&#39;,&#39;HTC_&#39;,&#39;Xda_&#39;,&#39;SAMSUNG-SGH&#39;,&#39;Wapaka&#39;,&#39;DoCoMo&#39;,&#39;iPhone&#39;,&#39;iPod&#39;);  
                    
        $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<count($list);$i++){
        if(strpos($str,$list[$i]) > 0){
            $flag = true;
            break;
        }
    }
    return $flag;
}
if(check_wap()){
    echo "wap";
}else{
    echo "web";
}
?>

Method 2:

<?php
function isMobile(){  
    $useragent=isset($_SERVER[&#39;HTTP_USER_AGENT&#39;]) ? $_SERVER[&#39;HTTP_USER_AGENT&#39;] : &#39;&#39;;  
    $useragent_commentsblock=preg_match(&#39;|\(.*?\)|&#39;,$useragent,$matches)>0?$matches[0]:&#39;&#39;;        
    function CheckSubstrs($substrs,$text){  
        foreach($substrs as $substr)  
            if(false!==strpos($text,$substr)){  
                return true;  
            }  
            return false;  
    }
    $mobile_os_list=array(&#39;Google Wireless Transcoder&#39;,&#39;Windows CE&#39;,&#39;WindowsCE&#39;,&#39;Symbian&#39;,&#39;Android&#39;,&#39;armv6l&#39;,&#39;armv5&#39;,&#39;Mobile&#39;,&#39;CentOS&#39;,&#39;mowser&#39;,&#39;AvantGo&#39;,&#39;Opera Mobi&#39;,&#39;J2ME/MIDP&#39;,&#39;Smartphone&#39;,&#39;Go.Web&#39;,&#39;Palm&#39;,&#39;iPAQ&#39;);
    $mobile_token_list=array(&#39;Profile/MIDP&#39;,&#39;Configuration/CLDC-&#39;,&#39;160×160&#39;,&#39;176×220&#39;,&#39;240×240&#39;,&#39;240×320&#39;,&#39;320×240&#39;,&#39;UP.Browser&#39;,&#39;UP.Link&#39;,&#39;SymbianOS&#39;,&#39;PalmOS&#39;,&#39;PocketPC&#39;,&#39;SonyEricsson&#39;,&#39;Nokia&#39;,&#39;BlackBerry&#39;,&#39;Vodafone&#39;,&#39;BenQ&#39;,&#39;Novarra-Vision&#39;,&#39;Iris&#39;,&#39;NetFront&#39;,&#39;HTC_&#39;,&#39;Xda_&#39;,&#39;SAMSUNG-SGH&#39;,&#39;Wapaka&#39;,&#39;DoCoMo&#39;,&#39;iPhone&#39;,&#39;iPod&#39;);  
          
    $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||  
              CheckSubstrs($mobile_token_list,$useragent);  
          
    if ($found_mobile){  
        return true;  
    }else{  
        return false;  
    }  
}
if (isMobile())
    echo &#39;手机登录&#39;;
else
    echo &#39;电脑登录&#39;;
?>

If you want to know more about programming learning, please pay attention to the php training column!

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

Related articles

See more