What's in a Name?

This week we'll explore how to take advantage of a little-used feature of CGI scripts. Let's start, however, with a more conventional example. Many times, a CGI script on a web server may be invoked with a URL such as:

http://server.dat/cgi-bin/script.pl?param=one&this=that

This URL, when passed to the web server, will invoke the script named script.pl which resides in your cgi-bin directory. (This might be /usr/local/etc/httpd/cgi-bin ... ask your system administrator or consult your web server's documentation.) Because this is a GET method, the environment variable QUERY_STRING will be set to whatever follows the ?, so in this case:

QUERY_STRING=param=one&this=that

It's then up to your script.pl to take that information from the environment and do something with it: This will produce the following output in your web browser:

You'll notice that the use of ? and & and other special symbols are defined in Internet standards documents. (Building a fully-featured parser for the CGI parameters is outside the scope of this article, however.)

If instead of spitting out text, maybe the CGI script should send an image. So, try something like this:

In this case, using some HTML code like the following will display an arbitrary image file. (Of course, this is just a silly example. You can undoubetly come up with a situation where this might actually be useful...) If, however, the user tries to save that image (by right-clicking on it in the web browser), the web browser will probably prompt for a name like "imgscript.pl" when saving the image. That isn't really a good choice for the filename, and in Windows, that .pl extension doesn't let you know it's really a jpeg image.

Instead, it would be nice if the web browser used what came after that ? for naming that saved file. That can be accompished using another another environment variable called PATH_INFO. Now, instead of using a ? to separate the CGI parameters from the script name, a / is used:
Here's the script to interpret that URL:
Now, if this file is saved, the name "bestimage.jpg" will be used, because /cgi-bin/imgscript.pl/bestname.jpg looks like the complete path and filename of bestname.jpg image.

Finally, if you're really adventerous, you can combine the two methods: Here's the Perl script that takes the size parameter and uses it to modify the filename: Now, this will look for the file named "big-bestimage.jpg" in the current directory.

Author: Doug Steinwand
Date: [09/23/97]

More articles about CGI
More articles by Doug Steinwand
Author Biography

/body>