当前位置:Gxlcms > PHP教程 > 检测useragent版本

检测useragent版本

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

检测注流浏览器及其版本
  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);

人气教程排行