Coding Domain

CGI Programming: What does CGI?


About CGI scripts
When you're experimenting with your websites, you might have noticed the term CGI somewhere. This article might enlarge your glance about the usage of CGI in websites, or at least give you an idea what CGI is.

Introduction to HTTP
Lot's of people have made some HTML code, made their website, and uploaded the HTML and images. Then anyone knowing your URL, can visit your page.

The webserver at the internet you posted the files at, handles the requests from the clients, and returning the pages you created. Then, the client can start interpreting the HTML code, and display the page. Strange as it seams, the webserver closes the connection as soon as the request is handled. Your internet connection is still open, but your HTTP connection to the webserver is closed! That means the webserver can handle other requests, by other computers. The number of connections a webserver can handle are limited.

Now let's see this in detail. At the internet, computers identify themselves with an IP address. Four numbers are difficult to remember, so humans can use a URL, which get converted to an IP Address. A DNS Server keeps up a list with such addresses. Using the IP address, we can connect with the webserver, and send a HTTP request. The server answers with a HTTP Reply, in which the document is included.

Connecting through HTTP

A Closer look at the HTTP Chat
This is a detailed HTTP conversation between the client and the server. We assume the connection is already open.

The Client sends:

GET http://www.testserver.com/file.html HTTP/1.0
Connection: Keep-Alive
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Charset: iso-8859-1,*,utf-8
Accept-Encoding: gzip
Accept-Language: en, en-GB, de, fr, ja, ko, zh
Host: www.testserver.com:80
User-Agent: Mozilla/4.51 [en]C-c32f40p  (WinNT; U)

After the request, we leave one line blank. This indicates the end of the request. Now, the server processes the request, and returns the following:

The Server replies:

GET http://www.testserver.com/file.html --> 200 OK
Date: Thu, 02 Sep 1999 19:54:39 GMT
Server: Netscape-Enterprise/3.5.1G
Content-length: 2222
Content-type: text/html
Last-Modified: Wed, 01 Sep 1991 17:12:03 GMT

<HTML>
  <HEAD>
    <TITLE> This is a test document </TITLE>
    . . .
What does CGI then?
Well, what does CGI do then? As you can see, the HTML page is just transfered. Most commercial webservers are configured that .cgi files are executed first by an interpreter, before they are tranfered. This is not default, but a configuration made at the webserver.

That means you can write a script which uses, for example perl, as the interpreter. This is our uploaded CGI program, named script.cgi.

#!/usr/bin/perl -w   # This tells UNIX what interpreter to use

print "Content-type: text/html\n\n";

$TheTime = localtime();
print "<HTML><HEAD><TITLE>CGI test program</TITLE></HEAD><BODY>";
print "<H1> Hello World </H1>\n";
print "You requested this file at: $TheTime";
print "</BODY></HTML>";
exit;

That's it! Your script is interpreted by /usr/bin/perl. The Content-type line is printed by the CGI script. The rest is handled by the webserver. We print some basic HTML code, include the current time in the code. The client received the printed HTML code, and the time is just hard-coded in the HTML lines. The client won't see any of the code, nor that the time is included later by the script.

This is the situation, I've left out the DNS lookup.

Request a CGI script

More Information
This might seem very cool, but what can you do with it? The CGI program do almost everyting at the webserver. It can look in databases, let the webserver send an e-mail, create a log file of users visiting a website or show a directory listing to the client browser.

This infinite power, however, is limited by the webserver. Most free webservers simply don't allow CGI programs. CGI can cause security leaks, so the webserver will simply not allow the CGI program to do everyting. The CGI programmer needs to take care of the precausions of the webserver's administrator. Most of the time, the Perl interpreter is used for CGI programs. There are also other options, but Perl is definitely the favorite choice.

Sometimes, you see a ?name=value&fsd=rf line at the end of the URL. That are the parameters for the CGI script. The script can decode the information, and consider what to do then.

What can you make or do with CGI?
So, basically, everything that requires or utilizes interaction with the webserver, using the webserver as a central location for distrubution of the data, pages and information. The webserver also stores all data in a database, that might be accessable through a interface generated by a CGI script.