There are several methods to get browser properties. php get_browser function is very useful as it return many browser properties.but In for get_browser to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.
output of get_browser php function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
[browser_name_regex] => ^mozilla/5.0 (windows; .; windows nt 5.1; .*rv:.*) gecko/.* firefox/0.9.*$ [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9* [parent] => Firefox 0.9 [platform] => WinXP [browser] => Firefox [version] => 0.9 [majorver] => 0 [minorver] => 9 [cssversion] => 2 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [backgroundsounds] => [vbscript] => [javascript] => 1 [javaapplets] => 1 [activexcontrols] => [cdf] => [aol] => [beta] => 1 [win16] => [crawler] => [stripper] => [wap] => [netclr] => |
But if you dont have permission to your php.ini file you can use the alternative
$_SERVER[‘HTTP_USER_AGENT’]
HTTP_USER_AGENT response on different browser
FIREFOX
1 |
Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0 |
CHROME
1 2 |
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 |
OPERA
Opera/9.80 (Windows NT 6.1) Presto/2.12.388 Version/12.15
SAFARI
1 2 |
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2 |
IE9
1 |
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) |
IE8
1 2 |
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0) |
Based on the above pattern I made a custom php function like get_browser ,
but not provide actually same data provided by get_browser. it can give you essential information like browser name and its version.
Response :
1 2 3 4 5 |
Array ( [browser] => Firefox [version] => 21.0 ) |
php function alternative to get_browser
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
function get_browser_properties(){ $browser =array(); $agent=$_SERVER[‘HTTP_USER_AGENT’]; if(stripos($agent,“firefox”)!==false){ $browser[‘browser’] = ‘Firefox’; // Set Browser Name $domain = stristr($agent, ‘Firefox’); $split =explode(‘/’,$domain); $browser[‘version’] = $split[1]; // Set Browser Version } if(stripos($agent,“Opera”)!==false){ $browser[‘browser’] = ‘Opera’; // Set Browser Name $domain = stristr($agent, ‘Version’); $split =explode(‘/’,$domain); $browser[‘version’] = $split[1]; // Set Browser Version } if(stripos($agent,“MSIE”)!==false){ $browser[‘browser’] = ‘Internet Explorer’; // Set Browser Name $domain = stristr($agent, ‘MSIE’); $split =explode(‘ ‘,$domain); $browser[‘version’] = $split[1]; // Set Browser Version } if(stripos($agent,“Chrome”)!==false){ $browser[‘browser’] = ‘Google Chrome’; // Set Browser Name $domain = stristr($agent, ‘Chrome’); $split1 =explode(‘/’,$domain); $split =explode(‘ ‘,$split1[1]); $browser[‘version’] = $split[0]; // Set Browser Version } else if(stripos($agent,“Safari”)!==false){ $browser[‘browser’] = ‘Safari’; // Set Browser Name $domain = stristr($agent, ‘Version’); $split1 =explode(‘/’,$domain); $split =explode(‘ ‘,$split1[1]); $browser[‘version’] = $split[0]; // Set Browser Version } return $browser; } |
Watch this php function live
How to use.
just simple : if you want to load the css based on browser version or name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$browser_properties = get_browser_properties(); if($browser_properties[‘browser’] =‘Firefox’ ) { ?> < ?php } else if($browser_properties[‘browser’] =‘Internet Explorer’ && $browser_properties[‘version’] < ‘9’ ) { ?> < ?php } ?> |
Hope it will help. any feedback for improvement is appreciable.
happy coding.