首页  >  文章  >  后端开发  >  php检测是否是移动设备的浏览器

php检测是否是移动设备的浏览器

WBOY
WBOY原创
2016-07-25 08:43:03905浏览

这段代码可以检测用户是否使用移动设备浏览网页,检验非常完整

  1. $mobile_browser = '0';
  2. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
  3. strtolower($_SERVER['HTTP_USER_AGENT']))){
  4. $mobile_browser++;
  5. }
  6. if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or
  7. ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
  8. $mobile_browser++;
  9. }
  10. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  11. $mobile_agents = array(
  12. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  13. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  14. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  15. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  16. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  17. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  18. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  19. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  20. 'wapr','webc','winw','winw','xda','xda-');
  21. if(in_array($mobile_ua,$mobile_agents)){
  22. $mobile_browser++;
  23. }
  24. if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
  25. $mobile_browser++;
  26. }
  27. if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
  28. $mobile_browser=0;
  29. }
  30. if($mobile_browser>0){
  31. // do something
  32. } else {
  33. // do something else
  34. }
  35. ?>
复制代码

php


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn