Copyright ©1996, Que Corporation. All rights reserved. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 West 103rd Street, Indianapolis, IN 46290 or at support@mcp .com.

Notice: This material is excerpted from Special Edition Using CGI, ISBN: 0-7897-0740-3. The electronic version of this material has not been through the final proof reading stage that the book goes through before being published in printed form. Some errors may exist here that are corrected before the book is published. This material is provided "as is" without any warranty of any kind.

CHAPTER 6 - Examples of Simple CGI Scripts

This chapter shows you examples of simple CGI scripts that you can deploy. Most of these scripts are simple to use and elegant in the way they work. There's nothing like a good program that does exactly what it's supposed to do and does it well.

As you go through this chapter, you'll see several working CGI scripts that may do basically the same thing for multiple CGI programming languages or hardware platforms. This chapter introduces you to many of the different flavors of CGI scripts.

Think of this chapter as a visit to a car dealer. In the years gone by, you didn't have many options when looking for a new car. You had a choice of a few colors and maybe a few options. Now, when you visit one of those new, huge "automotive malls," it's mind-boggling. What started out as a simple look at the new Gargantuan Motor Company's seven-door family hauler that seats nine, you instead find yourself looking at 20 variations of the seven-door rug-rat mobile, not to mention the many choices of colors.

In this chapter, you can do some CGI script window shopping and get in a few tire kicks. As you take this tour, you'll be introduced to many other uses for some of these scripts.

In this chapter, you'll see the following types of CGI scripts in action:

E-Mail Gateways

A couple of years ago, e-mail gateways were a hot topic on the Web. How do you make an HTML interface for sending e-mail from a client's Web browser to another host? CGI was the only option to use then. You didn't have these new Web browsers as you do today. Back then, people used various e-mail programs that ran on UNIX systems, with names such as elm, mail, and pine, to send e-mail to one another. Many old-timers still do.

Now the browsers have built-in e-mail systems to handle sending and receiving your e-mail. These systems can be called with a simple line of HTML code, as in the following:

<A HREF=""></A>

This capability solved the basic problem of needing a CGI script on your server to actually handle the e-mail problem. Most of you have seen this new way of handling mail within the browser. Figure 6.1 shows how the browser renders the URL tag of mailto. The ability to use mailto is a great new solution to the original problem of not being able to handle e-mail in a browser-but it creates only a minimalist mail form.

Fig. 6.1

In this figure, you see how Netscape Navigator interprets and handles the mailto URL.

At first glance, when you compare the example of a CGI e-mail gateway in figure 6.2 with the example of the built-in mail handling features of the browser shown in figure 6.1, they're about the same. However, the CGI version has an HTML interface that you can customize to have inline graphics and predefined text to create a look to match the Web site. You then have something much more than a simple mail form. You can create a custom HTML interface for the CGI e-mail form.

Fig. 6.2

This figure shows an example of an HTML interface for a CGI E-Mail gateway.

See  "Order Processing via Mail," , for additional information on using e-mail for online ordering. 
See "Setting Guestbook Script Options," , on how to use e-mail for sending thank-you notices for CGI scripts. 

You can find various versions of e-mail gateway scripts at the Web sites listed in table 6.1.

Table 6.1 Referenced Software Information

Name Language CD URL Perl Yes
email.tcl TCL Yes
mailcgi C Yes
email.cgi AppleScript Yes

Begin your CGI script window shopping by starting with, a widely used CGI script. Figure 6.3 shows a basic HTML form for this CGI script. Because this script is written in Perl, it can be used with a wide variety of operating systems. This script takes the simple HTML form input of a name, e-mail address, and a comments field, and uses the mail system on the Web server to send the message.

Fig. 6.3

This example of the CGI script's HTML form sends e-mail messages.

The next example of an e-mail CGI gateway is written in TCL (fig. 6.4 shows the HTML interface). Because the CGI code is compiled, it will run much faster compared to Perl-based scripts. You should consider running email.tcl if you plan to have a large amount of gated e-mail. The downside to this script is that TCL isn't for the novice, but requires a good working knowledge of C or C++.

Fig. 6.4

This is an example of the TCL-based email.tcl CGI script's HTML form interface.

If you're already familiar with C, you may want to look at mailcgi, which is written entirely in generic ANSI C (see fig. 6.5). Because it's written in C, if you're an accomplished C programmer, you should be able to port the CGI script fairly easy to most Web server platforms.

Fig. 6.5

Here' a simple HTML interface to the C-based CGI script called mailcgi.

The last, but not least, example of an e-mail CGI script is based on AppleScript (fig. 6.6 shows its HTML interface). This script will run only on Macintosh Web servers. If you use AppleScript, you might want to consider email.cgi.

Fig. 6.6

This simple HTML form interface is for the AppleScript-based e-mail gateway email.cgi.

Feedback or Comment Scripts

Feedback or comment scripts are normally a variation on the HTML CGI-based e-mail gateway scripts discussed in the preceding section. Feedback scripts normally send the client's comments via e-mail to the responsible recipient (see fig. 6.7). Sometimes the CGI scripts are modified to also handle appending the comments to a log file (see fig. 6.8) or sending the client another URL, or what I call a "thank-you" page (see fig. 6.9).

See  "E-mail Notification," , for more information on setting up the Guestbook CGI script to provide e-mail notification to the Webmaster. 

Fig. 6.7

This is a nicely done example of an HTML interface to a feedback or comments script.

Fig. 6.8

This example shows the HTML output of the Feedback script log file.

Fig. 6.9

Shown here is a simple HTML thank-you document that will be returned to users after they submit a feedback form.

When I ask someone to take time out of his or her life to fill out a feedback page, I like sending an HTML thank-you page back to that person. This not only acts as a reward-it's the polite thing to do.

If you decide to log the comments received from feedback into a publicly accessible document, remember to read the comments. Sometimes, certain individuals get carried away and use your comments log page as a public forum.

See  "Integrating CGI into Your HTML Pages," , for additional information on how to build HTML forms. 

You can find various versions of feedback and comment scripts at the Web sites listed in table 6.2. Table 6.2 Referenced Software Information

Name Language CD URL Perl Yes Perl Yes Perl No

A Visitor Guest Book Script

For a fun CGI script, consider putting a guest book CGI script on your Web site. A visitor guest book script is something you may want to consider implementing if you want to see who's actually visiting your Web site. With a guest book CGI script, the user fills out a simple HTML form like the one in figure 6.10. Then the client submits the HTML form to the CGI script for processing.

See  "Installing and Modifying a Guestbook CGI Script," , for background information on this script. 

Fig. 6.10

This is a nice HTML interface to a guest book script.

The guest book CGI script then takes the client's form and processes the input data into an HTML-based log. Then you can view this log with your browser (see fig. 6.11). The format of the HTML visitor log depends on how the author wrote the CGI script. Some guest book scripts do a nice job of formatting the HTML log file (as in fig. 6.11), and others are just too complex or cluttered when viewed (as in fig. 6.12). As with most simple CGI scripts, these types of problems can be handled with some simple modifications.

Fig. 6.11

This section of the guestbook.html log file was created with the Perl CGI script

Fig. 6.12

The HTML output of WGUESTBK created this section of the visitor's log file.

Another popular style of guest book on the Web is highly graphical. This CGI guest book, written by Brigette Jellinek, is comprised of several Perl scripts and is fabulous (see fig. 6.13). This guest book is much more than a visitor's log; it allows the user to click an image map that places a numbered marker at the visitor's geographic location. This number corresponds with the visitor's number in the rest of the log files (see fig. 6.14).

Fig. 6.14

This graphical guest book shows the geographic location of the visitor.

Fig. 6.13

This figure shows the visitor's comments and information, related to a graphically numbered index.

The Perl scripts used to create the graphics in figure 6.13 are-to put it simply-very complex. The output from Brigette's scripts is shown as an example of hard work when it comes to CGI scripting. As you can imagine, I could write another book on these Perl scripts alone.

See  Chapter 9, "Generating Images in Real Time," , for more information on some methods used in Brigette Jellinek's graphical guest book. 

"What's on the CD?" for more information. However, you should also check the Web sites listed in table 6.3 for updates and additional information.

Table 6.3 Referenced Software Information

Name Language CD URL Perl Yes
guest 2.0 Perl Yes
wguestbk Perl No

Page-Hit Counters

Page-hit counters are those nice little counting numbers in some HTML documents (see fig. 6.15). These counters show others how many other visitors, including themselves, have visited a Web page. Counters also seem to be a big hit with people getting started in CGI programming.

Fig. 6.15

Notice the graphic numbers at the bottom of this example, which was created by the page-hit counter Count WWWebula.

Counter Methods

There are two different ways of incrementing or tripping counters. The first one is by causing a CGI counter program to be run every time someone accesses the page. For example, look at the following HTML code:

<IMG SRC="/bin/counter.exe">

In this example, when someone requests the document containing this code, that person's browser will also request an image file named /bin/counter.exe to be loaded. (This really isn't an image file, but it causes the Web server to execute the program.) When the counter program runs, it reads a file containing a number, adds one to the total, and writes the new number back to the file. When the counter program knows this hit count number, it generates a graphics image of the number and sends this graphic to the waiting Web browser.

See  "Modifying Flexible Page-Hit Counters," , for more information on implementing inline graphic page-hit counters. 

By using a CGI script as the method for counting your page hits, and depending on the counter you use, you can customize the counter at a later time.

Another method of putting hit counts on a page is by using server-side includes. Chapter 16, "Using Server-Side Includes," provides more information.

Various CGI Counters

As you'll see, a wide variety of Web page hit counters is available for your use. Counter CGI scripts are slightly different from other CGI scripts in that they're written for a specific operating system and hardware. This is especially true when working with Windows NT versions of CGI counters.

Most CGI counters are written in C or C++. The authors of CGI counters use C/C++ because of the nature of the counter applications. They need to juggle vast amounts of graphic data around. Languages such as Perl and UNIX shell programming weren't really created to do that kind of graphic manipulation. Another reason the authors use C/C++ is because of performance considerations. Can you imagine what would happen if you had a slow CGI counter written in Perl? If the Web server had a fairly substantial amount of hits, server performance would drop dramatically.

The following sections take a look at some of the different C/C++-based hit counters.


For Mac-based Web servers, one of the best Web page-hit counters is Count WWWebula, which is conditional shareware. (Conditional shareware, in this case, means that if you're going to use this software for commercial or government agency Web servers, you need to send the author $25.) To see an example of Count WWWebula, refer to figure 6.15.

Because Count WWWebula is fast, it's great for servers where performance is a consideration. Count WWWebula is also "digits" compatible and guards against counter terrorism.

The more advanced CGI counters mentioned here will also prevent other Web sites from "stealing" your counter. Such stealing is called counter terrorism. This is where Webmaster wannabes reference someone else's page-hit counter, making it look like theirs, thus giving an artificial page-hit count.

Dig It: Digits

Some of the more advanced CGI counters mentioned here allow you to use individual number graphics. These graphics are referred to as digits.

A digit is the taking of a single decimal number and creating a stylized graphical image of that single number. And there needs to be a different graphical image, or digit, for each decimal number. After you create each decimal number in a similar styled digit, you can use a digit's compatible page-hit counter to take a stored decimal number, and then to have the counter to create one larger graphical image from each digit.

If you're performance-conscious or if your server hardware is underpowered, use a CGI counter software that has "built-in" digits. This means that the CGI scripts don't have to do various reads to the hard drive to construct the number count image. It has the information to construct the graphical image stored within the page-hit counter.

Windows NT

Windows NT-based Webmasters have several choices of CGI page-hit counters to choose from. Behold! Software makes a CGI counter that runs on Windows NT and Win95. The company also has specific CGI scripts for Windows NT running on Intel-, Alpha-, and MIPS-based hardware. (This CGI software is donation-ware; if you like the software and decide to use it, the writers of the software ask that you send them a donation.)

Behold! Software's CGI script works very well, and I have recommended it to many. This script is "digits" compatible and prevents counter terrorism. Figure 6.16 shows an example of a counter created with this software.

Fig. 6.16

This is an example of the usage of the Web page counter by Behold! Software.

Chris Babb is the author of another fast CGI counter that's written in VB. Babb's counter works well for those running Windows NT or Win95 where performance is an issue. This counter does one thing-simple counting-and does it very well (see fig. 6.17).

Fig. 6.17

Aquila Internet's home page uses Chris Babb's counter.

See  "The Windows Common Gateway Interface," , for more information on using VB applications as CGI scripts. 


In my opinion, one CGI-based hit counter for UNIX stands head and shoulders above the rest. Muhammad A. Muquit's WWW Homepage Access Counter and Clock absolutely is a Webmaster's dream counter. This counter doesn't use server-side includes and is very efficient. This CGI counter can display not only the hit counts, but also a real-time clock and the current date (see fig. 6.18).

Fig. 6.18

Notice that Muhammad A. Muquit's WWW Homepage Access Counter also works like a clock.

You can define a customizable 3-D frame around the counter and can make any of the colors transparent. You can specify the style of digits at runtime within the HTML code calling the CGI script (see fig. 6.19). The counter also allows you to handle any number of users for any number of Web pages. Your Web server users can even specify the initial number for the counter. You can even specify authorized host names and turn on IP filtering to prevent counter terrorism.

Fig. 6.19

Shown here are a few different examples of various styles of digits for Muhammad A. Muquit's WWW Homepage Access Counter.

See  "Frame Thickness and Colors," , on modifying Count v2.2. 
See  "Installing and Configuring Count v2.2," , on the modifications needed to implement Count v2.2 on your system. 

At the time of this writing, the most current version of this counter, version 2.2, is available only for UNIX-based Web servers. An older version, version 1.5, has been ported and made available for OS/2 and Windows NT-based Web servers. And since Muhammad distributes this CGI script as C source code only, and because the counter requires some custom set up, it may be a problem for some to modify the CGI script.

You can find these and other counter-related CGI scripts and digits at the Web sites listed in table 6.4.

Table 6.4 Referenced Software Information

Name Language CD URL
Count WWWebula C/Mac Only No
Web Page Counter C/NT Only Yes
Babb's Counter VB Yes
WWW Counter 1.5 C/NT-OS/2 Yes
WWW Counter 2.2 C/UNIX Yes
DigitMania N/A Yes

Web-Based Bulletin-Board Systems

An area that's beginning to grow on the Web is Web-based bulletin-board systems (BBSs). In the past, if your users wanted to have threaded discussions, they had only two choices: use a private newsgroup, or telnet into a BBS system on the Net. But neither option seems too great to us Webmasters. Hence, Web-based bulletin-board systems were created.

Web-based BBSs are relative newcomers, but this area is going to become one of the hottest areas on the Web. Why? Because Web BBSs allow questions, answers, and memos to be posted so that others can read and possibly answer them. Now in the corporate world, Lotus Notes is providing this form of communication. As the Web and Lotus Notes become more similar, more corporations, special-interest groups, technical support organizations, clubs, and others will use the Web for threaded, archived communications.

See  "The Next Step with CGI," , for more information on creating another basic communication medium. 

You have several choices, if you want a commercially available package. You can check out O'Reilly and Associates' WebBoard at here. But one of the best publicly available Web BBS software packages is Matt Wright's WWWBoard Version 2.0 (see fig. 6.20). WWWBoard is a set of Perl CGI scripts and HTML forms. Because the scripts are Perl, you can modify them, if needed, to run on any platform that has Perl available for it. You can find this script at

Fig. 6.20

A look at the HTML interface of various WWWBoard-posted messages also shows the replies, names, date, and time of the post.

See  "HTML-Based Chat Systems," , for alternatives to Web-based BBSs. 
See  "Perl CGI Examples," , for some considerations to WWWBoard. 

If you click the hyperlinked subject, you'll receive a message formatted something like the one in figure 6.21. From the messages screen, you can view or post a follow-up message. To post the follow-up message, you can scroll down and just enter the pertinent information into an HTML form (see fig. 6.22).

Fig. 6.21

This is the HTML interface of the WWWBoard message-posting area.

Fig. 6.22

This section of the WWWBoard message follow-up HTML form allows replies to be posted.

See "Integrating CGI into Your HTML Pages," , for a few ideas for HTML form interfaces. 
See  "The Access Configuration File," , for more information on adding access security to WWWBoard. 

WWWBoard also comes with a very usable administration CGI script called WWWAdmin (see fig. 6.23). One problem with BBSs is the administration: You always have someone posting a message that you need to remove. With WWWAdmin, you can remove unwanted postings in various ways-by message number, author, or date. As you can see in figure 6.24, you need to type in your user name and password. If you want to change your password, an HTML interface is included to handle that simple chore (see fig. 6.25).

Fig. 6.23

Here is a look at the WWWAdmin tools for WWWBoard.

Fig. 6.24

Shown here is WWWAdmin's HTML interface to remove messages by number.

Fig. 6.25

You can use this HTML form in the WWWAdmin area of WWWBoard to access the Change Password HTML form.

To keep up-to-date with WWWBoard, the URL of Matt's Script Archive is here..

Web-Based Games

With the advent of Image Map technology, you just knew someone was going to create some pretty cool games. Some of the CGI scripts that handle these games aren't what anyone would call simple, though. For example, take a look at figure 6.26, which shows a CGI implementation of Rubik's Cube.

Fig. 6.26

The famous Cube, in a CGI script written by Gid, is an example of an advanced CGI script.

If you want to see this CGI script in actions, point your browser to here. But note that this is a heavily hit site, and it could take a while to access.

See  "How an Image Map Functions," , and "Generating Images in Real Time," , for more information on the interface that works with the Cube. 
See  "Methods of Generating Real-Time HTML," , for more information on the technology behind the Cube CGI script. 
See  "The Next Step with CGI," , for additional thoughts on using CGI as an interface. 

Look at the simulation in figure 6.27. This is nothing more than a huge collection of basic HTML pages with a few image maps on it. It's a cool tour that shows you how far you can go with simplicity. The only complicated thing about this VR tour is the time it probably took to link all the HTML pages together and to scan in some photos.

Fig. 6.27

This example of image map techniques-an Abandoned Missile Base VR Tour-can be found at this location.

This goes to show you that you can create some in-depth "virtual" experiences with the basics of CGI technology.

For additional examples of games, you should check out Yahoo! Click the following at ( Yahoo ): Recreation, Games, Internet Games, or Interactive Web Games.

Previous Chapter <-- Table of Contents --> Next Chapter

QUE Home Page

For technical support for our books and software contact

Copyright ©1996, Que Corporation