Home > Backend Development > PHP Tutorial > Detect useragent version

Detect useragent version

WBOY
Release: 2016-07-25 08:48:31
Original
950 people have browsed it
检测注流浏览器及其版本
  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);
复制代码


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template