1. previous post: Barcelona Clubbers Take Chip Implants to Avoid Lines
  2. next post: Update for “IE7”

Browser Sniffing with PHP

Filed under “Web Design & Development
by Adam at 12:22 PM on June 2, 2004

3 Comments

I continue to marvel at the utility of PHP in web development.

I’ve noticed in my visitor stats that I get a surprising number of hits from people using antique web browsers that don’t support current web standards — like CSS — that are necessary to view this page as intended. Last month, for example, I saw the following:

Hits by Outdated Browsers, May 2004
Browser Hit Count
Internet Explorer < 6.0 4607
Internet Explorer 4.x 154
Netscape Navigator 4.x 638
WebTV 16

I’ve wanted to display a brief message to users of NN and IE 4.x urging them to upgrade, but leave that message out for other users. This is easy enough to do with NN 4.x — just use @import to bring in your style sheet, then include the upgrade message in a <div class="blank"> and specify .blank { display: none } in your style sheet. You could accomplish similar results with IE 5.x using conditional comments, but that doesn’t seem to work in IE 4.x. When it comes to WebTV I’m really out of luck, since it doesn’t even properly support JavaScript.

So what’s a web developer to do when they want to conditionally display content based on a visitor’s browser version? PHP to the rescue, baby. Some enterprising individual at the Engineering College of Denmark (the work is uncredited) has created a browser sniffer in PHP that lets your web server do the work of determining what program your visitor is using. Not only does this technique succeed where others fail, it also keeps your pages nice and lean without the added cruft of a bunch of JavaScript.

I love PHP. :-D

Note: A similar solution has been cooked up by a SourceForge project, but it doesn’t look quite as easy to implement.

Adam is a web developer and graphic designer who lives and works in south-central Kansas. He likes to speak his mind, both here and in his business blog. He only rarely writes about himself in the third person, honest. If you’d like to work with Adam, drop him a line.

3 Comments »

  1. Are you using the script then? I was wondering how easy it would be to adapt so that you could pull in different css files depending on which browser you’re using?

    Comment by Guy — August 13, 2004 @ 8:45 am

  2. Guy: I am not using the script myself. I got caught up in the transition from Movable Type to WordPress, and never managed to find time to implement the PHP sniffer. From looking over the sniffer’s web page, however, I think selective CSS might work something like this:

    <?php if ($browser['type'] == 'Netscape' && $browser['css'] == '2') { ?>
        <link rel="stylesheet" type="text/css" href="ns.css" media="screen" />
    <?php } elseif ($browser['type'] == 'Explorer' && browser['css'] == '2') { ?>
        <link rel="stylesheet" type="text/css" href="ie.css" media="screen" />
    <?php } elseif ($browser['css'] == '2' && (browser['dom'] == 'W3C' || browser['dom'] == 'IE')) { ?>
        <link rel="stylesheet" type="text/css" href="other.css" media="screen" />
    <?php } else { ?>
        <link rel="stylesheet" type="text/css" href="plain.css" media="screen" />
    <?php } ?>

    Hope that helps.

    Comment by Adam M. — August 13, 2004 @ 2:34 pm

  3. […] client detection. I posted on the topic in June, with links to a couple of ready-made PHP scripts. # February 1, 2005 02:00 PM 184 Mark Wubbenwrites: […]

    Pingback by Mike Davidson: sIFR 2.0 RC 3 is Here... Come Get Some — April 4, 2006 @ 7:04 pm

Say something, already

Line and paragraph breaks are automatic. Your e-mail address will never be published publicly unless you put it in your comment (and then I’d probably edit it out).

Please read my comment policy if this is your first time commenting here.

Required fields marked with *

*

* (never published)

Quicktags: