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();