设为首页收藏本站

PHPIN.NET

 找回密码
 立即注册
查看: 945|回复: 0

[PHP类\函数] PHP判断客户访问终端函数(手机访问判断)

[复制链接]

391

主题

398

帖子

2923

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2923
发表于 2015-4-20 17:47:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
PHP判断客户访问终端函数(手机访问判断)

is_mobile_request():
  1. /*检测手机访问*/
  2. function is_mobile_request(){
  3.     $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
  4.     $mobile_browser = '0';
  5.     if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT']))){
  6.         $mobile_browser++;
  7.     }
  8.     if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false)){
  9.         $mobile_browser++;
  10.     }
  11.     if(isset($_SERVER['HTTP_X_WAP_PROFILE'])){
  12.         $mobile_browser++;
  13.     }
  14.     if(isset($_SERVER['HTTP_PROFILE'])){
  15.         $mobile_browser++;
  16.     }
  17.     $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  18.     $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-' );
  19.     if(in_array($mobile_ua, $mobile_agents)){
  20.         $mobile_browser++;
  21.     }
  22.     if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false){
  23.         $mobile_browser++;// Pre-final check to reset everything if the user is on Windows
  24.     }
  25.     if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false){
  26.         $mobile_browser=0;// But WP7 is also Windows, with a slightly different characteristic
  27.     }
  28.     if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false){
  29.         $mobile_browser++;
  30.     }
  31.     if($mobile_browser>0){
  32.         return true;
  33.     }else{
  34.         return false;
  35.     }
  36. }
复制代码

使用方法:
  1. if (is_mobile_request()) {
  2.     //echo '当前为MOBILE<br />';
  3. } else {
  4.     //echo '当前为PC<br />';
  5. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|PHPIN.NET ( 冀ICP备00000001号 )|网站地图  

GMT+8, 2017-10-20 04:15

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表