the complete webmaster
tutorials reviews reference
ASP
CGI
FrontPage
HTML
Java
JavaScript

Sponsored by El Scripto

Visit the Mortgage Loan Place for Home Loans and also click here to find VA Loans on our site.

Get more website traffic

home / articles / cgi

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:
    #!/usr/local/bin/perl
    # a very simple example to display
    # query string from a url
    print "Content-type: text/plain\n\n",
      "QUERY_STRING:\n";
    @inarray=split(/&/,$ENV{'QUERY_STRING'});
    foreach (@inarray) {
        print "$_\n";
    }
    
This will produce the following output in your web browser:

    QUERY_STRING:
    param=one
    this=that
    
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:

    #!/usr/local/bin/perl
    # an example of spitting out a jpeg file
    
    $whichfile=$ENV{'QUERY_STRING'};
    
    print "Content-type: image/jpeg\n\n";
    open FILE, $whichfile;
    while (read FILE, $f, 16384) {
        print $f;
    }
    close FILE;
    
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...)
    <html><body>
      <img src="/cgi-bin/imgscript.pl?bestimage.jpg">
    </body></html>
    
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:
    <html><body>
      <img src="/cgi-bin/imgscript.pl/bestimage.jpg">
    </body></html>
    

Here's the script to interpret that URL:
    #!/usr/local/bin/perl
    # another example to spit out a jpeg file
    
    $whichfile=$ENV{'PATH_INFO'};
    
    print "Content-type: image/jpeg\n\n";
    open FILE, $whichfile;
    while (read FILE, $f, 16384) {
        print $f;
    }
    close FILE;
    
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:
    <html><body>
      <img src="/cgi-bin/imgscript.pl/bestimage.jpg?big">
    </body></html>
    
Here's the Perl script that takes the size parameter and uses it to modify the filename:
    #!/usr/local/bin/perl
    # an example of spitting out a jpeg file
    # using both PATH_INFO and QUERY_STRING
    
    $whichfile=$ENV{'QUERY_STRING'}."-".
                   $ENV{'PATH_INFO'};
    
    print "Content-type: image/jpeg\n\n";
    open FILE, $whichfile;
    while (read FILE, $f, 16384) {
        print $f;
    }
    close FILE;
    
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

Open software to automate your online business
write for us about us advertise

Copyright 1997, 1998 A Big Lime. All rights reserved.

/body>