This class can be used to detect the current user browser.
It takes the user agent header value and returns the identification of the detected browser.
The class may also return the name of a CSS file or PHP script associated to the current browser.
index.php
<?php
/*
class AutomaticBrowserDetect 0.1 by Roberto Aleman, you can use it with BSD Licence
Please use it, and report to me your experience for improvement the code in the next version */
include_once("automaticbrowsersdetect.php");
$newbrow = new AutomaticBrowsersDetect();
$newcss = new AutomaticBrowsersDetect();
$newurl = new AutomaticBrowsersDetect();
echo "<center><h2>Your favorite browser is : ".$newbrow->GetBrow($_SERVER['HTTP_USER_AGENT'])."</h2><br/>";
echo "<h2>I want Use this CSS styles : ".$newcss->checkcss($newbrow->GetBrow($_SERVER['HTTP_USER_AGENT']))."</h2><br/>"; // show correct css file
echo "<h2>I want Use this URL to show my favorite web content with my CSS styles : ".$newurl->checkurl($newbrow->GetBrow($_SERVER['HTTP_USER_AGENT']))."</h2>"; // show correct destination url
echo "<br/><br/>by Roberto Aleman, you can use it with BSD Licence<br/>Please use it, and report to me your experience for improvement the code in the next version</center>";
/* AND NOW BY EXAMPLE, YOU CAN REDIRECT TO YOU PREFER URL ACCORD THE USER AGENT!!! ENJOY!! */
?>
automaticbrowsersdetect.php
<?php
/*
class AutomaticBrowsersDetect 0.1 by Roberto Aleman, you can use it with BSD Licence
Please use it, and report to me your experience for improvement the code in the next version */
class AutomaticBrowsersDetect{
function GetBrow($ua) {
$bws = array(
'Flock' => '(Mozilla)(Gekco)(Firefox)',
'Chrome' =>'Chrome',
'Safari' =>'Safari',
'Opera' => 'Opera',
'MobileSafari'=> 'Apple', //iphone
'Opera Mini' => 'Opera Mini', //mobile phones with wap services
'Android' => 'Android', //android and nokia Nseries
'Mozilla Firefox'=> '(Firebird)|(Firefox)',
'Galeon' => 'Galeon',
'Mozilla'=>'Gecko',
'MyIE'=>'MyIE',
'Lynx' => 'Lynx',
'Netscape' => '(Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79)',
'Konqueror'=>'Konqueror',
'Internet Explorer 8' => '(MSIE 8\.[0-9]+)',
'Internet Explorer 7' => '(MSIE 7\.[0-9]+)',
'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
);
foreach($bws as $bw=>$mdl){
if (eregi($mdl, $ua))
return $bw;
}
return 'UnKnow';
}
function checkcss($brow){
$css = array(
'Flock' => 'Flock.css',
'Chrome' =>'Chrome.css',
'Safari' =>'Safari.css',
'Opera' => 'Opera.css',
'MobileSafari'=> 'Apple.css', //iphone
'Opera Mini' => 'OperaMini.css', //mobile phones with wap services
'Android' => 'Android.css', //android and nokia Nseries
'Mozilla Firefox'=> 'Firefox.css',
'Galeon' => 'Galeon.css',
'Mozilla'=>'Gecko.css',
'MyIE'=>'MyIE.css',
'Lynx' => 'Lynx.css',
'Netscape' => 'Netscape.css',
'Konqueror'=>'Konqueror.css',
'Internet Explorer 8' => 'ie8.css',
'Internet Explorer 7' => 'ie7.css',
'Internet Explorer 6' => 'ie6.css',
'Internet Explorer 5' => 'ie5.css',
'Internet Explorer 4' => 'ie4.css',
);
foreach($css as $k => $v) {
if($brow == $k){
return $v; }
}
}
function checkurl($brow)
{
$url = array(
'Flock' => 'Flock.php',
'Chrome' =>'Chrome.php',
'Safari' =>'Safari.php',
'Opera' => 'Opera.php',
'MobileSafari'=> 'Apple.php', //iphone
'Opera Mini' => 'OperaMini.php', //mobile phones with wap services
'Android' => 'Android.php', //android and nokia Nseries
'Mozilla Firefox'=> 'Firefox.php',
'Galeon' => 'Galeon.php',
'Mozilla'=>'Gecko.php',
'MyIE'=>'MyIE.php',
'Lynx' => 'Lynx.php',
'Netscape' => 'Netscape.php',
'Konqueror'=>'Konqueror.php',
'Internet Explorer 8' => 'ie8.php',
'Internet Explorer 7' => 'ie7.php',
'Internet Explorer 6' => 'ie6.php',
'Internet Explorer 5' => 'ie5.php',
'Internet Explorer 4' => 'ie4.php',
);
foreach($url as $k => $v) {
if($brow == $k){
return $v; }
}
Oswaldo Goite wrote :1900
Your code work PERFECT, I've tested it on a few browsers,
such as Firefox 3.5.8, Safari 4.0.4, Opera 10.50, Chrome
4.1.249.1036 and Internet Explorer 8. And It does what you
say it does.
But, there's something You may be aware of, when I tried it
in IE8, the text looks HUGE, it almost fills the screen
from left to right... I don't know if that's an issue with
your code or another stupidity from that browser... but...
It's just for you to know it...