Browser Detection Questions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Browser Detection Questions

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Why use browser detection at all?

The usual reason is so you can use specific features.. so why not use feature detection?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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).
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Re: Browser Detection Questions

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Perhaps if you tell us why you want to detect browsers, we can help on that front.
Post Reply