检测移动设备

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测移动设备相关的知识,希望对你有一定的参考价值。

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

以上是关于检测移动设备的主要内容,如果未能解决你的问题,请参考以下文章

检测安卓设备是不是具有移动数据功能

检测设备前后移动

如何检测手机或移动设备? [复制]

检测到 Googlebot Desktop 为移动设备

检测移动设备[重复]

如何检测安卓设备的移动?