Hudzilla.org - the homepage of Paul Hudson
Contents > Miscellaneous topics Wish List | Report Bug | About Me ]

16.5     Browser detection

This is NOT the latest copy of this book; click here for the latest version.

object get_browser ( [string user_agent])

You should already know that you can use the $_SERVER['HTTP_USER_AGENT'] variable to see what web browser requested a page, but how can you tell whether that browser supports the HTML IFRAME element? Does that browser support CSS 2? What version number is it?

While you could glean a little more information by actually parsing the user agent, a much better solution is to let PHP do it for you! This is done using the get_browser() function, which, when given no parameters, parses the user agent from $_SERVER['HTTP_USER_AGENT']. Alternatively, you can pass it a particular user agent to work with. Either way, it returns an object that contains information about that particular web browser: what it's capable of, what version it is, and what platform it's running on.

The function works by looking up the user agent in its long list of browsers, and reading from there what that browser supports. You need to download this browser list yourself - the latest URLs are always kept in the PHP manual, but last time I checked it was http://www.garykeith.com/browsers/downloads.asp . Look in the manual for the get_browser() function, then search the page for "browscap.ini" - the name of the browser capabilities file. Download the file (at the time of writing, the link on the PHP site takes you to another site that has a PHP-specific browscap.ini file) and place it either in c:\Windows for Windows users or /etc for Unix users, then open up your php.ini file. In there, search for the line "[browscap]" and edit the next line to point to your new browscap.ini file. So, it might look like this:

browscap = c:/windows/browscap.ini

Save the file and restart your web server to have the new browscap file loaded. Now, onto the code! The easiest way to get started is just to dump out all the information so you get an idea what's on offer:

>?php
    echo "
";
    print_r(get_browser());
    echo "
";
?>

That will print out the long list of information made available to you. Here are some of the things printed out for me:

[parent] => IE 6.0
[platform] => WinXP
[netclr] => 1
[browser] => IE
[version] => 6
[majorver] => 6
[minorver] => 0
[css] => 2
[frames] => 1
[iframes] => 1

From that you can see I'm running IE6 on Windows XP with .NET installed, it supports CSS v2.0, as well as HTML frames and IFRAME elements. There are quite a few other things there, too - try it yourself for more.

Now, just dumping out information like that is no use to anyone - what you really want to do is get down to the nitty-gritty of actually using that information in a smart way. For example, we could use it to load a custom CSS file for the various browsers out there. Keep in mind that each browser usually has its own way of rendering CSS styles, so having different stylesheets for each browser makes a lot of sense!

Here's some code to do just this:

<?php
    $browser
= get_browser();

    switch (
$browser->browser) {
        case
"IE":
            switch (
$browser->majorver) {
                case
6:
                case
5:
                    echo
'<LINK HREF="ie5plus.css" REL="stylesheet" TYPE="text/css" />';
                    break;
                default:
                    echo
'<LINK HREF="ieold.css" REL="stylesheet" TYPE="text/css" />';
            }

            break;

        case
"Firefox":
        case
"Mozilla":
            echo
'<LINK HREF="gecko.css" REL="stylesheet" TYPE="text/css" />';
            break;

        case
"Netscape":
            if (
$browser->majorver < 5) {
                echo
'<LINK HREF="nsold.css" REL="stylesheet" TYPE="text/css" />';
            } else {
                echo
'<LINK HREF="gecko.css" REL="stylesheet" TYPE="text/css" />';
            }
            break;

        case
"Safari":
        case
"Konqueror":
            echo
'<LINK HREF="gecko.css" REL="stylesheet" TYPE="text/css" />';
            break;

        case
"Opera":
            echo
'<LINK HREF="opera.css" REL="stylesheet" TYPE="text/css" />';
            break;

        default:
            echo
'<LINK HREF="unknown.css" REL="stylesheet" TYPE="text/css" />';
    }
?>

If I have to explain any of how that works, the PHP programming world is in serious trouble!





<< 16.4 Reflection   16.6 Arbitrary-precision mathematics >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Joe W. - 06 Jan 2009

Hello I have developed a little script to help aid in the effort to get folks upgraded from IE6. It is a free PHP script that uses PHP to detect IE6, throws a nice html,css error, offers information and an upgrade link for end user.

One file to copy and paste from, very easy to use.. check it out folks:

http://www.thatgrafix.com/php_detect/

*a ton of folks still use that darn browser so as developers we should continue to make our sites function in that arena but offer up upgrade solutions.

A PHP User - 06 Jan 2009

no only piece of it, if you see anyone using ie6, laugh at him

A PHP User - 06 Jan 2009

QUOTE: alapidus1215 - 14 Jul 2008

"Why is the comment that I posted months ago dated to the current timestamp?"

Today is the 14th of July 2008 and so is the date of your post.

theNoob - 06 Jan 2009

All of the comments are set to the current timestamp...apparently...

bill@noisepop.com - 06 Jan 2009

What is the easiest way to include the OS into the string?

A PHP User - 06 Jan 2009

Because the date stamps are screwed up.

And IE6 supports some CSS2, but not most of it. It claims to support more than it really does...

alapidus1215 - 06 Jan 2009

Why is the comment that I posted months ago dated to the current timestamp?

alapidus1215@gmail.com - 06 Jan 2009

IE6 supports CSS 2?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow