Home >Backend Development >PHP Tutorial >php detects whether it is a mobile device browser

php detects whether it is a mobile device browser

WBOY
WBOYOriginal
2016-07-25 08:43:03949browse

This code can detect whether the user is browsing the web using a mobile device. The verification is very complete

  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. ?>
Copy code

php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn