WebDeveloper.com ®: Where Web Developers and Designers Learn How to Build Web Sites, Program in Java and JavaScript, and More!   
Web Developer Resource DirectoryWebDev 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

    CSS
    Graphics
    HTML
    JavaScript
    XML
    Dreamweaver/FrontPage
    Multimedia
    Web Video
    General
    Accessibility

 Server-Side
  Development

    ASP
    Perl
    PHP
    .NET
    Java
    SQL
    Other

 Web Development
  Business Issues

    Business Matters
    Website Reviews

 E-Commerce
    Domain Names
    Search Engines

 Etc.
    Computer Issues
    Forum Software
    Feedback
    The Coffee Lounge



Script Downloads
Disable Form Buttons

Featured: July 23, 2008
Description: This script will disable your submit button in order to prevent multiple form submissions. Easy to implement.

Get Script

Hosting Search
Unix   Windows
PHP   Webmail

Sign up for the free WebDeveloper E-mail newsletter!


JupiterWeb Commerce
Partners & Affiliates
Partner With Us
Website Load Testing
Promos and Premiums
Best Price
Laptops
Calling Cards
Online Education
Logo Design
KVM over IP
Promotional Products
Baby Photo Contest
Prepaid Phone Card
Online Shopping
Corporate Awards
PDA Phones & Cases

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

Just because Web sites are easy to build these days, that doesn't mean it's easy to build a quality Web site that meets your business objectives.

Before developing your next Web site, or redesigning an existing site, download this Internet.com eBook to guide you through the process and plan your project, whether you're developing a site in-house or outsourcing the project.
Register now for your free Internet.com membership to download your complimentary eBook. Membership will also give you access to:

eBook library         Whitepapers         Webcasts
Newsletters         WinDrivers

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)



Acceptable Use Policy

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers