0
votes

I have this code bit to get which browser the user uses

$browserArray = array(
    'Windows Mobile' => 'IEMobile',
    'Android Mobile' => 'Android',
    'iPhone Mobile' => 'iPhone',
    'Firefox' => 'Firefox',
    'Opera' => 'OPR',
    'Google Chrome' => 'Chrome',
    'Internet Explorer' => 'MSIE',
    'Opera' => 'Opera',
    'Safari' => 'Safari'
); 
foreach ($browserArray as $k => $v) {

if (preg_match("/$v/", $agent)) {
     break;
}   else {
 $k = "Unknown";
}
} 
$browser = $k;

But I can't get Opera, it returns Opera as Chrome.

The agent for Opera is "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 OPR/18.0.1284.68"

How can I make it get that Opera is Opera and not Chrome?

2
I can't check properly right now, but I have a feeling OPR/18.0.1284.68 might be Opera's identifier (OPR) and version numberBojangles
There's a bit more to it than that anyway, have a look at stackoverflow.com/questions/8754080/…The Blue Dog

2 Answers

1
votes

You have two Opera keys in your array declaration:

'Opera' => 'OPR',
'Opera' => 'Opera',

Second one is overwriting firs, so effectively you array looks like:

$browserArray = array(
    'Windows Mobile' => 'IEMobile',
    'Android Mobile' => 'Android',
    'iPhone Mobile' => 'iPhone',
    'Firefox' => 'Firefox',
    'Opera' => 'Opera',
    'Google Chrome' => 'Chrome',
    'Internet Explorer' => 'MSIE',
    'Safari' => 'Safari'
);  

So you are missing OPR identifier and Chrome is the one matching your agent.

0
votes

Simpler way:

print_r(get_browser());

outputs something like that:

    Array
    (
    [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] =>
)