当前位置:Gxlcms > PHP教程 > php中HTTP_USER_AGENT判断手机类型的函数

php中HTTP_USER_AGENT判断手机类型的函数

时间:2021-07-01 10:21:17 帮助过:48人阅读

本文介绍下,在php中,用HTTP_USER_AGENT判断手机类型的函数,有需要的朋友,参考下。

有关判断手机类型的方法,程序员之家之前的文章有所提及。 相关参考文章: 判断是否手机访问 php判断是否手机访问的代码

用HTTP_USER_AGENT判断手机类型,代码如下:
  1. <!--?php
  2. /**
  3. * 判断手机类型
  4. * by bbs.it-home.org
  5. */
  6. function is_mobile_request()
  7. {
  8. $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
  9. $mobile_browser = '0';
  10. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap
  11. |phone|iphone|ipad|ipod|android|xoom)/i',
  12. strtolower($_SERVER['HTTP_USER_AGENT'])))
  13. $mobile_browser++;
  14. if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
  15. $mobile_browser++;
  16. if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
  17. $mobile_browser++;
  18. if(isset($_SERVER['HTTP_PROFILE']))
  19. $mobile_browser++;
  20. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  21. $mobile_agents = array(
  22. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  23. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  24. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  25. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  26. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  27. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  28. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  29. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  30. 'wapr','webc','winw','winw','xda','xda-'
  31. );
  32. if(in_array($mobile_ua, $mobile_agents))
  33. $mobile_browser++;
  34. if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
  35. $mobile_browser++;
  36. // Pre-final check to reset everything if the user is on Windows
  37. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
  38. $mobile_browser=0;
  39. // But WP7 is also Windows, with a slightly different characteristic
  40. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
  41. $mobile_browser++;
  42. if($mobile_browser-->0)
  43. return true;
  44. else
  45. return false;
  46. }
  47. ?>
附一个自己写的简单方法:
  1. <!--?php
  2. //能过http_user_agent检测是否手机访问
  3. $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
  4. $uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|
  5. philips|panasonic|alcatel|lenovo|cldc|midp|mobile|wap)/i";
  6. if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
  7. {
  8. $Loaction = 'wap/';
  9. if (!empty($Loaction))
  10. {
  11. ecs_header("Location: $Loaction\n");
  12. exit;
  13. }
  14. }
  15. ?-->

人气教程排行