Introduction To CGI

by John Coriglano

<jcorig@udel.edu>

CGI (Common Gateway Interface). What exactly is it? You've probably heard about it, but if you're not a web page creator, you might not know really what it is.

First, CGI is not a programming language. You can use just about any language you like to create CGI programs. The only requirements are that the language can:

By far, the two most popular languages being used for CGI are C and Perl. Personally, I prefer Perl, since it does not have to be compiled.

CGI programs are run on the server - the server does the work of setting up environment variables, running the program, and passing the output of the program to the client. So, for example, when you click on the Submit button of a form, the server collects all the data and sets up all the variables, then launches the program. It then takes the output of the program and passes that back to the client as an http document. Thus, you cannot run CGI programs without a server.

If you're not familiar with Perl, than you probably don't work with UNIX too much! Perl is very popular on UNIX systems. It is an interpreted language that has great file and string manipulation features. It is a very good choice for use with CGI. There is an Amiga port of Perl on Aminet.

Using CGI From Your Pages

There are three (3) steps to adding CGI to your (or your client's) web pages:

  1. Obtain permission from the ISP (Internet Service Provider)
  2. Create the CGI programs
  3. Add links to your CGI programs from the HTML documents
How to obtain permission depends on your ISP.

An Access Counter Example

I'm sure you have seen the ubiquitous access counters on web pages: You are hit #xxxxx since 01.02.95. Without getting into a debate about what purpose these things serve, let's go over the steps on how to add one to our web pages.

First, we need to decide what language to use. I choose Perl! The main benefit of Perl is that it does not have to be compiled. So, you can create the script on an Amiga and it should run on a UNIX box with no modifications.

How It Works

We are going to make a text-only counter (creating a graphic one is beyond the scope of this article). The counter will be updated whenever the script is executed. But here's the $64,000,000 question: how does our script get executed?

Many of those access counters you've seen require a special program that runs all the time on the ISP's machine. Many ISPs don't like that, and don't allow them so we are going to make a friendlier version.

This can be done one of two ways:

  1. A dummy home page.
  2. Make the script the home page
For the first method to work, simply create a page that only has a little welcome message (or whatever) and a link to our script. The HTML for this might be:
<html><head><title>"Welcome To Our Site"</title></head>
<body>
<img src="welcome.gif" alt="Welcome To Our Site">
<p>
<form action="welcome.cgi" method="POST">
<input type="submit" value="Press To Continue">
</form>
</body></html>

And you might get a page that looks like this:
Example Page Image
When somebody visits our page, they will have to click the button to keep going. When they do that, the Perl script welcome.cgi will get executed.

To do the second method, just tell people to go directly to the Perl script when they want to access your home page. For example, if we are located at "nowhere.cool.com" and we have our script in the /cgi-bin directory, we would tell everyone that our home page is

http://nowhere.cool.com/cgi-bin/welcome.cgi

This will take them directly to the script, bypassing the welcome page.

Well, what about the script?

Now we finally get to the actual script. All this program does is it creates an HTML document on the fly. The actual documment will be coded directly into the program, but it could also be kept in a disk file.

In order to make the document, the script just needs to print the lines of the document to stdout and the server takes that and passes it along to the client as an HTML doc. The only prerequisite is that the very first thing that the program prints must be:

Content-type: text/html\n\n

Note that there are two (2) newline characters!! This is very important.

After this, the program just prints out the document, inserting the correct count in the right place. So (drum roll please), here's the script:

#!/usr/bin/perl

# The file that holds the number of hits to date
$cnt_file = "/home/myplace/counter"

# Initialize the counter in case we can't get the file
$cnt = 0;

# First, open the file that has the current counter value.
# This file should have one line (but we check them all) and
# the format of that line should be:
#  COUNTER: x
# Where x is the number of accesses so far.
if open(CNTER, $cnt_file)) {
    while (<CNTER>) {
        if (/^COUNTER: (\d*)/) {
            $cnt = $1;
        }
    }
    close(CNTER);
}

# Increment the counter
$cnt++;

# Write the new value back to the file for next access
if (open(CNTR, ">".$cnt_file)) {
    print CNTR "COUNTER: $cnt\n";
    close(CNTR);
}

# Now all that's left is to print out the HTML code
# Can't forget this!
print "Content-type: text/html\n\n";

# Now for the HTML
print "<html><head><title>Access Page</title></head>\n"
print "<h1> Welcome To The Access Page </h1>\n";
printf "You are visitor %05d to this page!\n", $cnt;
print "Check out our stock:\n";
print "<ul>\n";
print "<li> <a href=\"/home/my_home/widgets.html\"> Widgets </a>\n";
print "<li> <a href=\"/home/my_home/gizmos.html\"> Gizmos </a>\n";
print "<li> <a href=\"/home/my_home/doohickeys.html\"> Doohickeys </a>\n";
print "</ul>\n";
print "</body></html>\n"

And you should get something like this (except with real links):


Welcome To The Access Page

You are visitor 00123 to this page. Check out our stock:
Well, I guess that's all for now! Until next time, here are some books you might want to check out:
Table of Contents