首页  >  文章  >  后端开发  >  php如何判断是手机访问网站还是电脑访问网站

php如何判断是手机访问网站还是电脑访问网站

coldplay.xixi
coldplay.xixi原创
2020-09-07 09:33:342588浏览

php判断是手机访问网站还是电脑访问网站的方法:首先打开终端编辑器;然后输入判断代码【CheckSubstrs($mobile_token_list,$useragent)】;最后输出结果即可。

php如何判断是手机访问网站还是电脑访问网站

相关学习推荐:php编程(视频)】

php判断是手机访问还是电脑访问网站的方法:

方法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";
}
?>

方法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;;
?>

想了解更多编程学习,敬请关注php培训栏目!

以上是php如何判断是手机访问网站还是电脑访问网站的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn