note 91648 added to function.get-browser
| From: | StevePerkins at osu1 dot php dot net | Date: | Sun, 21 Jun 2009 16:42:30 +0000 |
| Subject: | note 91648 added to function.get-browser | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157197@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 extends BaseObjects_PropertyArray
{
private $props = array("Version" => "0.0.0",
"Name" => "unknown",
"Agent" => "unknown",
"AllowsHeaderRedirect" => true) ;
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 ;
}
}
$this->AllowsHeaderRedirect = !($this->Name == "msie" &&
$this->Version < 7) ;
}
public function __Get($name)
{
if (!array_key_exists($name, $this->props))
{
die "No such property or function $name)" ;
}
return $this->props[$name] ;
}
}
?>
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
----
Server IP: 209.41.74.194
Probable Submitter: 82.36.72.125
----
Manual Page -- http://www.php.net/manual/en/function.get-browser.php
Edit -- https://master.php.net/note/edit/91648
Del: integrated -- https://master.php.net/note/delete/91648/integrated
Del: useless -- https://master.php.net/note/delete/91648/useless
Del: bad code -- https://master.php.net/note/delete/91648/bad+code
Del: spam -- https://master.php.net/note/delete/91648/spam
Del: non-english -- https://master.php.net/note/delete/91648/non-english
Del: in docs -- https://master.php.net/note/delete/91648/in+docs
Del: other reasons-- https://master.php.net/note/delete/91648
Reject -- https://master.php.net/note/reject/91648
Search -- https://master.php.net/manage/user-notes.php