Forum Moderators: coopster

Message Too Old, No Replies

Detecting Opera, Firefox and IE7 - Not working

Need help on this one?

         

tomda

9:15 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have made recently three websites that used AJAX.
For compatibility issues, I want to give AJAX version to Opera, Firefox and IE7 and standard HTML version to other browsers.

So, I was looking for a small function (and not a huge class) that can detect Opera, Firefox and IE7. I found one but it is not working for Opera. Usually, I am able to solve PHP problems myself, but this time I have failed... I am completely lost with those string functions...


function browser_info() {
$browser = array ("MSIE", "OPERA", "MOZILLA", "NETSCAPE", "FIREFOX", "SAFARI");
$info['browser'] = "OTHER";
foreach ($browser as $parent) {
$s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent);
$f = $s + strlen($parent);
$version = substr($_SERVER['HTTP_USER_AGENT'], $f, 5);
$version = preg_replace('/[^0-9,.]/','',$version);
if (strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent)) {
$info['browser'] = $parent;
$info['version'] = $version;
}
}
return $info;
}

Anyway, using Firefox, print_r($info) returns Array ( [browser] => FIREFOX [version] => 2.0. ) but using Opera, it returns Array ( [browser] => OTHER ) instead of OPERA + VERSION...

Any idea what's wrong with the above function?
Or any links/resources (in PM please) for a similar functions?

Thanks

omoutop

10:19 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



in opera you get an option on how you wish the server to detect it: either IE, Opera, Firexof (press F12->site preferences)

Check these options in the browser and try again your script

Having my Opera detect as IE or Firefox, your script worked perfectly

Having my Opera detect as Opera, your script gave me the "Other" choise

echoing my $_SERVER['HTTP_USER_AGENT'] i got: Opera/9.21 (Windows NT 5.1; U; en)

Your script should work, but i cant find any flaws... strange. Sorry i couldt help

tomda

10:37 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Omoutop!

I did exactly the same than what you did... This script works perfectly for Firefox and Internet Explorer AND it was also working with previous version of Opera 8.

But from Opera 9.21, it doesn't work...
Very strange indeed knowing that HTTP_USER_AGENT is a plain "Opera/9.21" (no funny characters).

Your script should work, but i cant find any flaws... strange. Sorry i couldt help

Same here... I hope a PHP Guru will find the bug.
My last option is to track down the author of the script, may be hard and time consuming.

coopster

4:02 pm on Sep 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



At first glance I noticed you are using strpos [php.net] and there is a "gotcha" associated with this function. You should be testing for an exact boolean match because if the user-agent string is in the very first position, strpos() is going to return the index of 0, which is exactly where it is located but will indeed throw your logic off.
This ...
if (strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent)) {

... should be something like ...
if (strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent) !== false) {

See the manual page link for more information on this common mistake.

tomda

12:44 pm on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Coopster.
You are the best!

It works and I have taken note of warning on the manual...
It must a very common mistake since the warning is framed in a red box.

Much appreciated.
Tomda