首页 >后端开发 >php教程 >检测useragent版本

检测useragent版本

WBOY
WBOY原创
2016-07-25 08:48:31948浏览
检测注流浏览器及其版本
  1. define('IE', 1);
  2. define('FIREFOX', 2);
  3. define('CHROME', 3);
  4. define('OPERA', 4);
  5. class UserAgentDetect
  6. {
  7. static $version = array();
  8. static $userAgent = '';
  9. static function getUserAgent()
  10. {
  11. $header = getallheaders();
  12. self :: $userAgent = $header['User-Agent'];
  13. }
  14. function findUserAgentByKey($ua, $key)
  15. {
  16. $len = strlen($key);
  17. $start = strpos($ua, $key);
  18. $pos = strpos($ua, ' ', $start + $len + 1);
  19. return substr($ua, $start + $len, $pos - $len - $start);
  20. }
  21. static function detectUserAgent()
  22. {
  23. self :: getUserAgent();
  24. if (strpos(self :: $userAgent, 'MSIE') != null)
  25. {
  26. self :: $version[] = IE;
  27. self :: $version[] =self:: findUserAgentByKey(self :: $userAgent, 'MSIE');
  28. }
  29. else if (strpos(self :: $userAgent, 'Gecko/') != null)
  30. {
  31. self :: $version[] = FIREFOX;
  32. self :: $version[] =self:: findUserAgentByKey(self :: $userAgent, 'Firefox/');
  33. }
  34. else if (strpos(self :: $userAgent, 'AppleWebKit/') != null)
  35. {
  36. self :: $version[] = CHROME;
  37. self :: $version[] = self:: findUserAgentByKey(self :: $userAgent, 'Chrome/');
  38. }
  39. else if (strpos(self :: $userAgent, 'Presto/') != null)
  40. {
  41. self :: $version[] = OPERA;
  42. self :: $version[] = self:: findUserAgentByKey(self :: $userAgent, 'Opera/');
  43. }
  44. else
  45. {
  46. }
  47. return self :: $version;
  48. }
  49. function matchUserAgent($ua, $key)
  50. {
  51. preg_match("/$key.([^\s]*?)\s/", $ua, $out);
  52. return $out[1];
  53. }
  54. }
  55. $ua=UserAgentDetect::detectUserAgent();
  56. print_r($ua);
复制代码


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