Detect User Browser in PHP

Lately post is about detecting user’s component in PHP, now is about user’s browser, i used this to make some code’s condition, if its chrome its shows this and etc mostly is about layout. Here is the code :


function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = ‘Unknown’;

if(preg_match(‘/MSIE/i’,$u_agent) && !preg_match(‘/Opera/i’,$u_agent))
{
$bname = ‘Internet Explorer’;
$ub = “MSIE”;
}
elseif(preg_match(‘/Firefox/i’,$u_agent))
{
$bname = ‘Mozilla Firefox’;
$ub = “Firefox”;
}
elseif(preg_match(‘/Chrome/i’,$u_agent))
{
$bname = ‘Google Chrome’;
$ub = “Chrome”;
}
elseif(preg_match(‘/Safari/i’,$u_agent))
{
$bname = ‘Apple Safari’;
$ub = “Safari”;
}
elseif(preg_match(‘/Opera/i’,$u_agent))
{
$bname = ‘Opera’;
$ub = “Opera”;
}
elseif(preg_match(‘/Netscape/i’,$u_agent))
{
$bname = ‘Netscape’;
$ub = “Netscape”;
}
return $ub;
}

$ua = getBrowser();
if($ua == “Chrome”)
{
echo “Chrome”;
}else if($ua == “MSIE”){
echo “Internet Explorer”;
}else{
echo “Others”;
}

It helps me detected user’s browser so i can make changes to several browser.

Leave a Reply

Your email address will not be published. Required fields are marked *