Previous Message
Next Message

Re: [css-d] Determining Browser Type

Sent by Jonas Voss on 1 January 2003 13:01


On Mon, 30 Dec 2002 13:10:48 -0500, basil crow wrote:

> If you're going to use PHP:
> 
> <?php
> if (preg_match("/opera/i",$_SERVER["HTTP_USER_AGENT"])) {
>     $browser = "opera";
> } else if (preg_match("/gecko/i",$_SERVER["HTTP_USER_AGENT"])) {
>     $browser = "gecko";
> } else if (preg_match("/msie.[4|5|6]/i",$_SERVER["HTTP_USER_AGENT"])) {
>     $browser = "ie";
> } else if (preg_match("/nav/i",$_SERVER["HTTP_USER_AGENT"]) ||
> preg_match("/Mozilla\/4\./", $_SERVER["HTTP_USER_AGENT"])) {
>     $browser = "netscape";
> } else {
>     $browser = "other";
> }
> ?>
> 
> This is a very basic (but also very useful script). Your $browser will
> contain "opera", "gecko" (mozilla, chimera, netscape 6/7, phoenix, etc),
> "ie", "netscape" (note this is Netscape 4 or earlier, Netscape 6/7 fall
> under gecko for obvious reasons), or "other".

The problem, as I see it, with the above script is, that your MSIE-filter 
will remove MSIE 5 for mac as well as IE 5 for PC. IE 5 for mac supports a 
lot of the CSS specs out there.

Not to make this into a contest, but I am currently using this PHP-script 
I put together for the purpose of filtering browsers based on the user-
agents ability to render proper CSS. Now, I am a beginner with PHP, so 
bear with me if the code isn't all Daddy-O, but it works.

<?php
# Get user agent string from browser:
$b = $_SERVER['HTTP_USER_AGENT'];

# Commence sniffing:
function snif($b) {
    if (((stristr($b, "msie 6.0")) || (stristr($b, "msie 5.0")) || 
(stristr($b, "msie 5.5")) || (stristr($b, "msie 4.01"))) && (stristr($b, 
"windows"))){
        # If we are dealing with a windows-based IE browser, we return 
'Win_IE' as handle:
        return Win_IE;
    } elseif (stristr($b, "opera")) {
        # If it's an Opera beast, we dig deeper:
        if (stristr($b, "macintosh")) {
            # Hey, it's a mac Opera:
            return mac_Opera;
        } elseif (stristr($b, "Opera 7.0")) {
            # Hey it's Opera v.7:
            return Opera_7;
        } else {
            # Hey, it's just Opera:
            return Opera;
        }
    } elseif (stristr($b, "msie") && stristr($b, "mac_powerpc")) {
        # We are dealing with a Mac IE. I assume version 5 at least, hence 
no version checking. Deal with it:
        return mac_IE;
    } elseif (((stristr($b, "mozilla/5.0")) || (stristr($b, "gecko")))) {
        # We are dealing with a gecko based browser:
        if (stristr($b, "omniweb")) {
            # Oh my, it's OmniWeb = not CSS2 compliant, at all:
            return omni;
        } else {
            # Either Netscape 6+, Chimera, Phoenix, Galeon, Mozilla. In 
other words, CSS2 compliant:
            return gecko;
            }
    } elseif (stristr($b, "konqueror")) {
        # Konqueror is down here, as it doesn't ID itself as a gecko 
growser [not all the time anyway]. Better separate it from the rest then, 
in case it needs a brush-up later on.
        return konq;
    }
}
    
# If the browser visiting belongs to a CSS2-compliant family, they are 
served a cup of black coffee.
if (snif($b) == gecko || snif($b) == mac_IE || snif($b) == konq) {
    echo ("\t<link rel=\"stylesheet\" href=\"/css/coffee.css\" 
type=\"text/css\" />\n");

# If it belongs to the challenged family, they get served a decaf:
} elseif (snif($b) == Opera || snif($b) == mac_Opera || snif($b) == 
Opera_7 || snif($b) == Win_IE || snif($b) == omni) {
    echo ("\t<link rel=\"stylesheet\" href=\"/css/decaf.css\" 
type=\"text/css\" />\n");

# If they are none of the above, like Netscape 4.xx, links, lynx, etc., 
they are served didley.
} else {
    echo "";
}
?>

~/j

-- 
// Jonas C. Voss // [EMAIL-REMOVED] // http://verture.net/
Previous Message
Next Message

Message thread:

Possibly related: