Dave's Brain

Browse - Programming Tips - On a server-side CGI script how do I detect which browser the user is running?

Date: 2008apr11
Level: Beginner
Language: perl
Keywords: agent

Q.  On a server-side CGI script how do I detect which browser the user is running?

A.  

Please note that its best to use feature detection rather than browser
detection.  Maybe the next version of IE will have that feature
you assume it doesn't have.

Here is some simple perl code that checks to see if the user has a
particular browser.  This is usually want you want.

sub isFirefox() { return $ENV{HTTP_USER_AGENT} =~ m/Firefox/; } sub isIE() { return $ENV{HTTP_USER_AGENT} =~ m/MSIE/; } sub isAndroid() { return $ENV{HTTP_USER_AGENT} =~ m/Android/; } # Use like this... sub main() { print "Content-Type: text/plain\n\n"; if (isFirefox()) { print "You seem to be using Firefox\n"; } elsif (isIE()) { print "You seem to be using Microsoft Internet Explorer\n"; } else { print "You're not using Firefox or MSIE\n"; } } main();

Comment

From: hey
Date: 2009nov25 19:34
Subject: Thanks

Thanks for the info dave!

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.