Browser Auto Detection

There has been a lot written on the web about detecting a user’s browser. The reason why there is so much discussion is that Internet Explorer and Netscape have differences in the way that they render HTML and so the “experience” you get can be different for the same site. At least that was the reason so many people worried about it. Now that Internet Explorer commands over 90% of the browser market it is hardly worth worring about.

However, there are other reasons for detecting a user’s browser, such as redirecting to a different site or set of pages for a device such as a PDA or a Smartphone. This is what we needed to do at work and what I wanted to achieve on this site.

The way to detect the user’s browser is via what is called the User Agent. This is transmitted in the header of every request that your browser makes. If your browser has JavaScript support then you can see your user agent below:

document.write(navigator.appVersion)

If you see nothing then click here.

Using the information contained within the user agent it is possible to determine the device that the user is accessing the site from – this could be anything from a Palm, Pocket PC, Symbian Phone right through to a desktop browser – and then redirect to the correct location. This is what I have now implemented on this site.

When you access the site the first thing that happens is that your user agent is checked and then a decision made as to where to route you. This is all done with a simple Perl script as follows:


# Get the User Agent string
$usragnt = $ENV{'HTTP_USER_AGENT'};

# Make an intelligent stab at what is the calling device and redirect
if ((index($usragnt, “BlackBerry”) >= 0) ||
(index($usragnt, “Smartphone”) >= 0) ||
(index($usragnt, “Palm”) >= 0) ||
(index($usragnt, “Symbian”) >= 0) ||
(index($usragnt, “SonyEricsson”) >= 0) ||
(index($usragnt, “SHARP”) >= 0) ||
(index($usragnt, “240×320”) >= 0)) {

print “Location: http://www.neilthompson.co.uk/gm/pda.htmnn”;

}
else {

print “Location: http://www.neilthompson.co.uk/index.htmnn”;

}

# All Done
exit;
If you have an Internet enabled device – give it a go and let me know what you think.

Leave a Reply

Your email address will not be published. Required fields are marked *