Browser detection

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!

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Arbitrary-precision mathematics >>

Previous chapter: Reflection

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.