PHP判断是否为手机访问

        有时候咱们须要判断访问是来自手机端仍是PC端,PHP能够很方便的进行判断。
css

        $_SERVER['HTTP_USER_AGENT']中含有访问的头信息(header),根据它咱们能够作出判断 。浏览器

        function isMobile()less

        {  spa

        $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';  code

        $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';    token

        function CheckSubstrs($substrs,$text)get

        {  it

        foreach($substrs as $substr)  io

        if(false!==strpos($text,$substr)){  function

        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;  

        }  

        }

        这个方法稍加改造也能够判断是否用某种浏览器打开。

        原文连接:http://back.zhizhi123.com/?p=124

相关文章
相关标签/搜索