开发者

Help with Browser detection code

开发者 https://www.devze.com 2023-04-04 03:45 出处:网络
I have a problem with the line 36 of this code. <?PHP class Browser { private $props= array(\"Version\" => \"0.0.0\",

I have a problem with the line 36 of this code.

<?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 ;
    }

}

?> 

this is the line that have the error die "No such property or function $name" ; any help would be appropriated.


The line needs to be this, instead:

die("No such property or function $name");

See here for more info.


Why don't you rewrite that code to this?:

<?php

class Browser
{

    public $Version = "0.0.0";
    public $Name = "unknown";
    public $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 ;
            }
        }
    }
}

?>

Anyways, I suggest you using some already-existing library to detect browser, like http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

0

精彩评论

暂无评论...
验证码 换一张
取 消