Browser Redirection With CGI
by Jonathon Miller
If there is one major problem with the Web today, it is the fact that those who develop for it must work around browser incompatibilities. These differences are not just between the newest versions of competing software, such as Navigator and Internet Explorer, but also among different versions from the same company. Web developers are often left with broken tables, defunct style sheets, and Dynamic HTML that is anything but dynamic.
If you know what your reader is using to view your pages, they can be designed to take maximum advantage of that technology, no matter what the level. Browser detection is the best way to ensure your message is coming across the way you envisioned it.
That's where we come in.
The most common way of detecting a visitor's browser is via an index.cgi file written in Perl. If you have no idea what CGI or Perl are, take some time to read our Perl primer before continuing. Go ahead, I'm not going anywhere.
Okay, let's get started on that script.
#!/usr/local/bin/perl
The first line of any friendly Perl script is the location of the Perl compiler. A common variation of the above is #!/usr/bin/perl. To locate your Perl compiler, issue the command which Perl at the Unix prompt.
($TEST = $ENV{'HTTP_USER_AGENT'});
This next line grabs the HTTP_USER_AGENT environment setting from your browser and stores it in the variable $TYPE. This environment setting is the equivalent of clicking the help / about menu item of your browser, bringing up the name and version number of the client you use to surf the Net.
$netscape_3 = "http://www.yoursite.com/netscape3/";
$netscape_4 = "http://www.yoursite.com/netscape4/";
$iexplorer = "http://www.yoursite.com/ie/";
$lynx = "http://www.yoursite.com/lynx/";
$aol = "http://www.yoursite.com/aol/";
These five lines setup a URL for each browser you wish to test for. This will become more clear after reading the following code:
$browser = $netscape_3 if $TEST =~ /Mozilla/3/;
$browser = $netscape_4 if $TEST =~ /Mozilla/4/;
$browser = $lynx if $TEST =~ /Lynx/i;
$browser = $iexplorer if $TEST =~ /microsoft/i;
$browser = $aol if $TEST =~ /aol/i;
If you remember, we set $TEST equal to whatever string the browser returned when we asked for its version. In this snippet of code, we search $TEST for certain words associated with different browsers, such as 'microsoft' or 'aol' and set $BROWSER accordingly.
print "Location: $browser";
The final line of our script simply sends the reader to the URL specified in the string $browser, and then exits.
#!/usr/local/bin/perl
($TEST = $ENV{'HTTP_USER_AGENT'});
$mysite = "http://url.to.your.site";
$netscape_3 = "$mysite/netscape3/";
$netscape_4 = "$mysite/netscape4/";
$iexplorer = "$mysite/ie/";
$lynx = "$mysite/lynx/";
$aol = "$mysite/aol/";
$browser = $netscape_3 if $TEST =~ /Mozilla/3/;
$browser = $netscape_4 if $TEST =~ /Mozilla/4/;
$browser = $iexplorer if $TEST =~ /microsoft/i;
$browser = $lynx if $TEST =~ /Lynx/i;
$browser = $aol if $TEST =~ /aol/i;
print "Location: $browser"; exit();
Simply insert that into a text file named index.cgi and you're all finished. Welcome to the wide world of browser redirection. (You will need to chmod 755 index.cgi)
|