免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2174 | 回复: 0
打印 上一主题 下一主题

PHP判断用户是否手机访问的代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-22 17:20 |只看该作者 |倒序浏览
用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面。这里就介绍一下,如何判断用户是否用手机访问。
代码:
  1. $agent = check_wap();

  2. if( $agent )

  3. {

  4.   header('Location: http://www.aichengxu.com/view/61539');

  5.   exit;

  6. }

  7. // check if wap

  8. function check_wap(){

  9.   // 先检查是否为wap代理,准确度高

  10.   if(stristr($_SERVER['HTTP_VIA'],"wap")){

  11.       return true;

  12.   }

  13.   // 检查浏览器是否接受 WML.

  14.   elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){

  15.       return true;

  16. }

  17. //检查USER_AGENT

  18. elseif(preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){

  19.       return true;           

  20.   }

  21.   else{

  22.       return false;  

  23. }

  24. }
复制代码
判断函数
  1. function is_mobile_request()  

  2. {  

  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. if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))  

  8.   $mobile_browser++;  

  9. if(isset($_SERVER['HTTP_X_WAP_PROFILE']))  

  10.   $mobile_browser++;  

  11. if(isset($_SERVER['HTTP_PROFILE']))  

  12.   $mobile_browser++;  

  13. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));  

  14. $mobile_agents = array(  

  15.       'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',  

  16.       'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',  

  17.       'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',  

  18.       'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',  

  19.       'newt','noki','oper','palm','pana','pant','phil','play','port','prox',  

  20.       'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',  

  21.       'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',  

  22.       'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',  

  23.       'wapr','webc','winw','winw','xda','xda-'

  24.       );  

  25. if(in_array($mobile_ua, $mobile_agents))  

  26.   $mobile_browser++;  

  27. if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)  

  28.   $mobile_browser++;  

  29. // Pre-final check to reset everything if the user is on Windows  

  30. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)  

  31.   $mobile_browser=0;  

  32. // But WP7 is also Windows, with a slightly different characteristic  

  33. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)  

  34.   $mobile_browser++;  

  35. if($mobile_browser>0)  

  36.   return true;  

  37. else

  38.   return false;  

  39. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP