PHP Php浏览器检测类

Posted

tags:

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

<?php
class Browser
{
    private $props    = array("Version" => "0.0.0",
                                "Name" => "unknown",
                                "Agent" => "unknown") ;

    public function __Construct()
    {
        $browsers = array("firefox", "msie", "opera", "chrome", "safari",
                            "mozilla", "seamonkey",    "konqueror", "netscape",
                            "gecko", "navigator", "mosaic", "lynx", "amaya",
                            "omniweb", "avant", "camino", "flock", "aol");

        $this->Agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        foreach($browsers as $browser)
        {
            if (preg_match("#($browser)[/ ]?([0-9.]*)#", $this->Agent, $match))
            {
                $this->Name = $match[1] ;
                $this->Version = $match[2] ;
                break ;
            }
        }
    }

    public function __Get($name)
    {
        if (!array_key_exists($name, $this->props))
        {
            die ("No such property or function $name") ;
        }
        return $this->props[$name] ;
    }

    public function __Set($name, $val)
    {
        if (!array_key_exists($name, $this->props))
        {
            SimpleError("No such property or function.", "Failed to set $name", $this->props) ;
            die ;
        }
        $this->props[$name] = $val ;
    }

}
$Browser = new Browser;
?>

<?php //usage
  echo "$Browser->Name $Browser->Version" ;
?>

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

PHP PHP浏览器检测

php 使用PHP自动检测浏览器语言

PHP PHP浏览器检测指示灯

检测 PHP 中的浏览器语言

PHP 浏览器检测和重定向

用 php 检测浏览器的最佳方法是啥?