欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-17 08:20:13
...

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

  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