Reputation: 5007
I am trying to write a little script to find out what type of browser the user is using.
I found the following from somewhere (I think it was SO):
$browser = array(
'version' => '0.0.0',
'majorver' => 0,
'minorver' => 0,
'build' => 0,
'name' => 'unknown',
'useragent' => ''
);
$browsers = array(
'firefox', 'msie', 'opera', 'chrome', 'safari', 'mozilla', 'seamonkey', 'konqueror', 'netscape',
'gecko', 'navigator', 'mosaic', 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol'
);
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];
$user_agent = strtolower($browser['useragent']);
foreach($browsers as $_browser) {
if (preg_match("/($_browser)[\/ ]?([0-9.]*)/", $user_agent, $match)) {
$browser['name'] = $match[1];
$browser['version'] = $match[2];
@list($browser['majorver'], $browser['minorver'], $browser['build']) = explode('.', $browser['version']);
break;
}
}
}
var_dump($browser);
Out of all the little code snippets I tried, this one worked the best. It correctly identified Chrome and Firefox. However, It can't correctly identify IE, (I am using IE11) because IE doesnt pass any information to the browser. The output from the above code on IE11 is as follows:
array(6) {
["version"]=>
string(3) "5.0"
["majorver"]=>
string(1) "5"
["minorver"]=>
string(1) "0"
["build"]=>
NULL
["name"]=>
string(7) "mozilla"
["useragent"]=>
string(68) "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
}
Once again, Microsoft have to throw a spanner in the works when it comes to Internet Explorer. What can I do to detect IE? There is no MSIE or even any indication of Internet Explorer in the HTTP_USER_AGENT. How can I detect it?
Upvotes: 0
Views: 139
Reputation: 96241
Trident
is the rendering engine of IE – so whenever that partial string shows up, you can assume that you’re dealing with an Internet Explorer (or someone trying to fake one, of course).
Upvotes: 1