Php浏览器检测类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Php浏览器检测类相关的知识,希望对你有一定的参考价值。

Detect and echo browser version including differentiating between Chrome and Safari.
  1. <?php
  2. class Browser
  3. {
  4. private $props = array("Version" => "0.0.0",
  5. "Name" => "unknown",
  6. "Agent" => "unknown") ;
  7.  
  8. public function __Construct()
  9. {
  10. $browsers = array("firefox", "msie", "opera", "chrome", "safari",
  11. "mozilla", "seamonkey", "konqueror", "netscape",
  12. "gecko", "navigator", "mosaic", "lynx", "amaya",
  13. "omniweb", "avant", "camino", "flock", "aol");
  14.  
  15. $this->Agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  16. foreach($browsers as $browser)
  17. {
  18. if (preg_match("#($browser)[/ ]?([0-9.]*)#", $this->Agent, $match))
  19. {
  20. $this->Name = $match[1] ;
  21. $this->Version = $match[2] ;
  22. break ;
  23. }
  24. }
  25. }
  26.  
  27. public function __Get($name)
  28. {
  29. if (!array_key_exists($name, $this->props))
  30. {
  31. die ("No such property or function $name") ;
  32. }
  33. return $this->props[$name] ;
  34. }
  35.  
  36. public function __Set($name, $val)
  37. {
  38. if (!array_key_exists($name, $this->props))
  39. {
  40. SimpleError("No such property or function.", "Failed to set $name", $this->props) ;
  41. die ;
  42. }
  43. $this->props[$name] = $val ;
  44. }
  45.  
  46. }
  47. $Browser = new Browser;
  48. ?>
  49.  
  50. <?php //usage
  51. echo "$Browser->Name $Browser->Version" ;
  52. ?>

以上是关于Php浏览器检测类的主要内容,如果未能解决你的问题,请参考以下文章

如何使用模块化代码片段中的LeakCanary检测内存泄漏?

AJAX相关JS代码片段和部分浏览器模型

Php浏览器检测类

前端开发常用代码片段(中篇)

PHP简单性能检测类工具

PHP链式操作通过call和callstatic魔术方法的实现,以及phpstorm通过注释去追踪function