Page 1 of 1

Browser Detection Questions

Posted: Sun Apr 09, 2006 1:23 pm
by bruceg
I am using the following PHP script to detect a users browser while viewing my site

Code: Select all

<?php
if (strstr ($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
    echo '<p>I see you are using <strong> Internet Explorer</strong> .</p>';
}
elseif  (strstr ($_SERVER["HTTP_USER_AGENT"], "Firefox")) {
    echo '<p>I see you are using <strong>Firefox</strong>.</p>';
}

elseif  (strstr ($_SERVER["HTTP_USER_AGENT"], "Mozilla")) {
    echo '<p>I see you are using <strong>Mozilla</strong>. </p>';
}

elseif  (strstr ($_SERVER["HTTP_USER_AGENT"], "Opera")) {
    echo '<p> I see you are using <strong>Opera </strong>. </p>';
}

elseif  (strstr ($_SERVER["HTTP_USER_AGENT"], "Netscape")) {
    echo '<p> I see you are using <strong>Opera </strong>. </p>';
}
elseif  (strstr ($_SERVER["HTTP_USER_AGENT"], "Safari")) {
    echo'<p> I see you are using <strong>Safari</strong>. </p>';
}
else {
  echo '<p>I see you are using some other web browser. </p>';
}
?>
works fine for MSIE and Mozilla/Firefox, but it interprets Netscape as MSIE and Safari as Mozilla. Opera is interpreted as MSIE also.

Is this a limitation of $_SERVER["HTTP_USER_AGENT)] or can alter the script to get it to work for the intended browser detection?

thanks

Posted: Sun Apr 09, 2006 1:37 pm
by d3ad1ysp0rk
Why use browser detection at all?

The usual reason is so you can use specific features.. so why not use feature detection?

Posted: Sun Apr 09, 2006 1:46 pm
by feyd
Most (nearly all) browsers masquerade as more than one browser in their user agent string.

This may be of interest: viewtopic.php?t=46587

Posted: Sun Apr 09, 2006 2:11 pm
by Oren
Ah... this is a well known thing. What you need to do is to move this code:

Code: Select all

if (strstr ($_SERVER["HTTP_USER_AGENT"], "MSIE")) {

echo '<p>I see you are using <strong> Internet Explorer</strong> .</p>';

}
to the bottom of the list (the if's list).

Re: Browser Detection Questions

Posted: Sun Apr 09, 2006 3:19 pm
by AKA Panama Jack
bruceg wrote:I am using the following PHP script to detect a users browser while viewing my site

Code: Select all

<?php
elseif  (strstr ($_SERVER["HTTP_USER_AGENT"], "Opera")) {
    echo '<p> I see you are using <strong>Opera </strong>. </p>';
}
?>
works fine for MSIE and Mozilla/Firefox, but it interprets Netscape as MSIE and Safari as Mozilla. Opera is interpreted as MSIE also.

Is this a limitation of $_SERVER["HTTP_USER_AGENT)] or can alter the script to get it to work for the intended browser detection?

thanks
With programs like Opera you can't really detect it anymore because the User agent can be changed to ANYTHING. You can enter the url opera:config and directly edit the user agent so it is IDENTICAL to any other browser.

Opera also has a quick select under the Tools/Quick Preferences menu that will allow you to select the user agent you want to use for Opera. So you can select to identify yourself as Opera, Mozilla or IE.

I always get a chuckle out of the people who know very little about Opera claiming Opera is losing its share of the browser market when in fact it has always been gaining. They don't realize that most Opera users are maquerading as IE users with a significanly smaller portion masquerading as Mozilla. :D

Some of us are rebels, like myself, and always identify as Opera. :twisted:

The main reason so many Opera users identify as IE is because in the PAST quite a few sites would specifically look for Opera and feed them bad page layouts (MSN) or prevent them from accessing their site. Changing Operas user agent was a way to get around that. In the past year or two the only sites I have seen that exclude Opera are those written by very rabid anti-Opera programmers. I think I have run into maybe 3 or 4 sites like that in the past year and I surf everywhere. ;)

User agent detection is one of those things that is going to be almost a useless thing to do in the future as more browsers allow you to change the user agent string.

Posted: Sun Apr 09, 2006 3:53 pm
by John Cartwright
Perhaps if you tell us why you want to detect browsers, we can help on that front.