欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP中通过HTTP_USER_AGENT判断是否为手机移动终端的函数代码

程序员文章站 2022-05-27 19:37:45
有时候很实用在一些场合,留住备用吧 复制代码 代码如下:function is_mobile_request()     &nbs...

有时候很实用在一些场合,留住备用吧

复制代码 代码如下:

function is_mobile_request()  
    {  
      $_server['all_http'] = isset($_server['all_http']) ? $_server['all_http'] : '';  
      $mobile_browser = '0';  
      if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_server['http_user_agent'])))  
        $mobile_browser++;  
      if((isset($_server['http_accept'])) and (strpos(strtolower($_server['http_accept']),'application/vnd.wap.xhtml+xml') !== false))  
        $mobile_browser++;  
      if(isset($_server['http_x_wap_profile']))  
        $mobile_browser++;  
      if(isset($_server['http_profile']))  
        $mobile_browser++;  
      $mobile_ua = strtolower(substr($_server['http_user_agent'],0,4));  
      $mobile_agents = array(  
            'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',  
            'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',  
            'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',  
            'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',  
            'newt','noki','oper','palm','pana','pant','phil','play','port','prox',  
            'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',  
            'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',  
            'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',  
            'wapr','webc','winw','winw','xda','xda-'  
            );  
      if(in_array($mobile_ua, $mobile_agents))  
        $mobile_browser++;  
      if(strpos(strtolower($_server['all_http']), 'operamini') !== false)  
        $mobile_browser++;  
      // pre-final check to reset everything if the user is on windows  
      if(strpos(strtolower($_server['http_user_agent']), 'windows') !== false)  
        $mobile_browser=0;  
      // but wp7 is also windows, with a slightly different characteristic  
      if(strpos(strtolower($_server['http_user_agent']), 'windows phone') !== false)  
        $mobile_browser++;  
      if($mobile_browser>0)  
        return true;  
      else 
        return false;  
    }

文章来源:http://yi1.com.cn/posts/781

php 根据http_user_agent 判断用户浏览器类型

复制代码 代码如下:

function browsers(){
   global $http_user_agent ;
   if (isset($http_user_agent)){
    $sagent = $http_user_agent;
   }else{
    $sagent = $_server['http_user_agent'];
   }
   if (strpos($sagent,'msie') !== false && strpos($sagent,'mac') === false && strpos($sagent,'opera') === false){
    $iversion = (float)substr($sagent,strpos($sagent,'msie') + 5,3);
    return ($iversion >= 5.5) ;
   }else if (strpos($sagent,'gecko/') !== false){
    $iversion = (int)substr($sagent,strpos($sagent,'gecko/') + 6,8);
    return ($iversion >= 20030210) ;
   }else{
    return false;
   }
  }