note 91655 modified in function.get-browser by danbrown
| From: | danbrown@php.net | Date: | Mon, 22 Jun 2009 12:49:44 +0000 |
| Subject: | note 91655 modified in function.get-browser by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157225@lists.php.net to get a copy of this message | ||
This is a simple class to detect the client browser and version using regular expressions.
<?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 ;
}
}
?>
example code
<?PHP
$browser = new Browser ;
echo "$Browser->Name $Browser->Version" ;
?>
result when client using Firefox 3.0.11
firefox 3.0.11
result when client using unknown browser
unknown 0.0.0
etc etc
--was--
That's really bizarre. It posted an older version that I tried to post before, but which failed
at the time. Anyway, here it is again, this is the proper version.
This is a simple class to detect the client browser and version using regular expressions.
<?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 ;
}
}
?>
example code
<?PHP
$browser = new Browser ;
echo "$Browser->Name $Browser->Version" ;
?>
result when client using Firefox 3.0.11
firefox 3.0.11
result when client using unknown browser
unknown 0.0.0
etc etc
http://php.net/manual/en/function.get-browser.php