WebDeveloper.com �: Where Web Developers and Designers Learn How to Build Web Sites, Program in Java and JavaScript, and More!   
Web Developer Resource Directory WebDev Jobs
Animated GIFs
CSS
CSS Properties
Database
Design
Flash
HTML
HTML 4.01 Tags
JavaScript
.NET
PHP
Reference
Security
Site Management
Video
XML/RSS
WD Forums
 Client-Side
  Development

    HTML
    XML
    CSS
    Graphics
    JavaScript
    ASP
    Multimedia
    Web Video
    Accessibility
    Dreamweaver
    General
    Accessibility
    Dreamweaver
    Expression Web

    General

 Server-Side
  Development

    PHP
    Perl
    .NET
    Forum, Blog, Wiki & CMS
    SQL
    Java
    Others

 Site Management
    Domain Names
    Search Engines
    Website Reviews

 Web Development
  Business Issues

    Business Matters

 Etc.
    The Coffee Lounge
    Computer Issues
    Feedback




Browser Redirection with Perl

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 "Beginners Guide to CGI in Perl" 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 on 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)


HTML5 Development Center


Recent Articles