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:
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.
There are three (3) steps to adding CGI to your (or your client's) 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.
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:
<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:

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.cgiThis will take them directly to the script, bypassing the welcome page.
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\nNote 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):