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

检测useragent版本

程序员文章站 2022-05-03 14:29:22
...
检测注流浏览器及其版本
  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);
复制代码
相关标签: 检测useragent版本