Get browser info quick function

There are more than one way to get the browsers information with PHP, so to make it simple, I have created this function that will check if the ini configuration is set to get the browser info, and if is not, then it will parse the $_SERVER variable to get the correct browser information.

function getBrowserInfo(){
$isBrowserCapSet = ini_get(‘browscap’);
$browser = ”;
if(strlen($isBrowserCapSet) > 0){
$browserInfoAsArray = get_browser(null,true);
if(isset($browserInfoAsArray[‘browser’])){
return $browserInfoAsArray[‘browser’];
}
}
else {
$browserInfoAsArray = explode(‘ ‘,$_SERVER[‘HTTP_USER_AGENT’]);
if(isset($browserInfoAsArray[9]) && preg_match(‘/[a-zA-z]+/’,$browserInfoAsArray[9])>0){
$browserInfoAndVersion = explode(‘/’,$browserInfoAsArray[9]);
if(isset($browserInfoAndVersion[0])){
return $browserInfoAndVersion[0];
}
}
else if(isset($browserInfoAsArray[2])){
return $browserInfoAsArray[2];
}
}
return $_SERVER[‘HTTP_USER_AGENT’];
}

P.S. This is not tested on all browsers.

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

2 responses to “Get browser info quick function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: