internet.com

Go to WebDeveloper Home


hm-v6-139x61.gif

Database Journal: The Knowledge Center for Database Professionals

find a web host with:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
jobs

Get FREE Development Info via your Email!

The Webdeveloper channel
FlashKit
GIF.com
HiermenusCentral
webdeveloper Jobs
Java Boutique
JavaScript.com
JavaScript Source
ScriptSearch
StreamingMedia World
WDJ
WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com

internet.com
Internet News
Internet Investing
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

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

internet.commerce
Be a Commerce Partner










Library

HTML Tips and Tricks

by Scott Clark

The Client Is Always Right!
Part 3

CGI to the Rescue

I know there are some of you out there that are saying, "Well, that's fine for you, but my employer says that if it doesn't work on 99% of the browsers out there, he doesn't want to use it." For those in this category, CGI is the answer to your prayers. Using Perl (which has even been ported from Unix to Windows NT), you can detect the client's browser type and direct the client to the appropriate page.

#!/usr/bin/perl
#
# referer.pl
$browser = $ENV{'HTTP_USER_AGENT'};
if (index($browser,"MSIE") >= 0) {
        print "Location: http://webdeveloper.com/msie.html\n\n";
} elsif (index($browser,"Mozilla") >= 0) {
        print "Location: http://webdeveloper.com/netscape.html\n\n";
} elsif (index($browser,"Mosaic") >= 0) {
        print "Location: http://webdeveloper.com/mosaic.html\n\n";
} else {
        print "Location: http://webdeveloper.com/oldbrowser.html\n\n";
} 
exit(0);

Recognize our "userAgent" from the previous JavaScript example? Independent of the method you use to access it, userAgent tells you the same basic client information. Basically, what referer.pl does is get the userAgent variable from the header:

$browser = $ENV{'HTTP_USER_AGENT'};

If the browser is Microsoft Internet Explorer, then it displays the MSIE page:

if (index($browser,"MSIE") 
>= 0) {
        print "Location: http://webdeveloper.com/msie.html\n\n";

It continues though the different browsers until it finds one that meets the criteria it has (namely that it is either Netscape, Explorer, Mosaic, or something else), and sends the client to the appropriate page:

} elsif 
(index($browser,"Mozilla") 
>= 0) {
        print "Location: http://webdeveloper.com/netscape.html\n\n";
} elsif (index($browser,"Mosaic") >= 0) {
        print "Location: http://webdeveloper.com/mosaic.html\n\n";
} else {
        print "Location: http://webdeveloper.com/oldbrowser.html\n\n";
exit(0);

The above CGI script has to be in the CGI-BIN directory of your Web server in order to work. If the CGI example above was called referer.pl, it would be referenced by a URL, e.g. http://www.yoursite.com/cgi-bin/referer.pl, and when the client accessed the URL, they would be directed to the appropriate page according to the Web browser they were using. Although this URL would work fine as a link inside a site, it's not exactly a URL you'd want to give out on your business cards.

One way to get around that is to redirect the client to the referer.pl URL from the index.html page, which would take them to the correct page for their browser. Most Web servers allow you to redirect clients from one URL to another, and in this case you set up your server to redirect the URL from index.html to /cgi-bin/referer.pl.

Doing Less Extra Work

What you have to ask yourself is whether or not it's worth creating different versions of your page or site for each browser. A more efficient solution would be to create two pages, one that is enhanced with Netscape and Internet Explorer tags, and one that is plain HTML.

Using CGI, direct those with Netscape and Explorer to the enhanced page, and "other browsers" to the plain HTML page. On the enhanced page, use the JavaScript example above, or a variant of it, to find out what version of browser visitors have, and use JavaScript to create the page with the browser/version-specific tags and effects. Using this method, you can create a very unique page for each browser, optimized for the particular version that the viewer is using. This puts you on that bleeding edge once again--at least for a fleeting moment.


Scott Clark (sclark@webdeveloper.com) is Technical Director for Web Developer.com®. He is based in Kissimmee, Florida, and when he's offline (which isn't often), you'll find him digging in the dirt, working with his tropical plants.

Reprinted from Web Developer® magazine, Vol. 3 No.1 Jan/Feb. 1997 (c) 1997 internet.com Corporation. All rights reserved.


[ Click here to move to the first part of the article ]

Web Developer® Site Feedback


Web Developer®
Copyright © 2000 internet.com Corporation. All rights reserved.

Web Developer® Home Over a dozen topics in detail Live Chat Downloads Book and Product Reviews Threaded Discussions How-To/Articles/Links Developer Daily News Subscribe Search Corporate Information Advertise Events Publications internet.com Home