加入收藏 | 设为首页 | 会员中心 | 我要投稿 西安站长网 (https://www.029zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP根据HTTP_USER_AGENT判断客户端访问能否为手机访问

发布时间:2022-01-21 20:09:36 所属栏目:PHP教程 来源:互联网
导读:现在web发展速度非常快,各种响应式的站点越来越多,除了APP外,在平时可能需要根据客户终端访问来加载不同时模板或跳转到不同时的域名下,现在分享一个函数,可以做到这样判断.android、ios和wp都已经测试过. PHP实例代码如下: //判断moblie function is_mobile(
  现在web发展速度非常快,各种响应式的站点越来越多,除了APP外,在平时可能需要根据客户终端访问来加载不同时模板或跳转到不同时的域名下,现在分享一个函数,可以做到这样判断.android、ios和wp都已经测试过.
 
  PHP实例代码如下:
 
  //判断moblie
  function is_mobile()
  {
      $_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|ios)/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;
  }
 

(编辑:西安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读