WWW: Beyond the Basics

12. Common Gateway Interface

12.4. Examples

12.4.1. Hello World

12.4.1.1. Description / Motivation

This CGI application is designed to say "hello" to its viewer. It takes no input, and the only output it provides is the HTML to display a simple message. This example demonstrates sever important aspects of CGI applications:

12.4.1.2. Source Code

Click here to view the source code

12.4.1.3. Test the Example

Click here to run helloworld.cgi


12.4.2. Date and Time

12.4.2.1. Description / Motivation

This CGI application displays the date and time (relative to the web server on which the CGI application executes). The capabilities demonstrated by this CGI application are:

12.4.2.2. Source Code

Click here to view the source code

12.4.2.3. Test the Example

Click here to run date.cgi


12.4.3. Finger gateway

12.4.3.1. Description / Motivation

This CGI application is a gateway to the "finger" utility. The finger system, when queried about a certain user, returns information about that user (such as their real name, or their office telephone number). So, this finger gateway provides a WWW interface to this system. This example demonstrates:

12.4.3.2. Source Code

Click here to view the source code

12.4.3.3. Test the Example

Click here to obtain the author's finger information


12.4.4. Access Counter

12.4.4.1. Description / Motivation

This example presents a rough yet functional access counter. The CGI application in this example doesn't return any HTML code or images -- instead, it returns pointers to GIF images. A WWW document that wishes to contain this counter includes in its source n images, each of which has its source specified by this CGI. Each of these n images is a digit in the count of accesses to the parent document. The SRC attribute of the IMG tag which displays these images is a call to counter.cgi; in this call, a query is made which specifies the digit to be displayed. This query is in the form of digit=i, where i is the 10i place. For example:

<IMG SRC="http://csgrad.cs.vt.edu/vanmetre-cgi/counter.cgi?digit=1">

will display the digit in the tens place.

Whenever a page asks counter.cgi for a digit, the URL of the referring page is passed to counter.cgi in the HTTP_REFERER environment variable. Each time the ones digit for a URL is accessed, counter.cgi increments the access count for the referring URL and stores this value in a database on the host server. This is an example of a desirable side effect -- a global variable (the database file) is being modified, and these modifications will affect all others who access this global object at a later time. This example demonstrates the following features of CGI applications:

12.4.4.2. Source Code

Click here to view the HTML source code
Click here to view the CGI source code

12.4.4.3. Test the Example

Click here to view count-me.html

[PREV][NEXT][UP][HOME][VT CS]

Copyright © 1996 J. Patrick Van Metre, All Rights Reserved

J. Patrick Van Metre <vanmetre@csgrad.cs.vt.edu>
Last modified: Sat Oct 26 13:26:04 1996