by Michael Erwin
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 has wound up being a look 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:
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 today's Web browsers. 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:
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.
Figure 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.
Figure 6.2 This figure shows an example of an HTML interface for a CGI e-mail gateway.
You can find various versions of e-mail gateway scripts on the
accompanying CD-ROM and at the Web sites listed in table 6.1.
Begin your CGI script window shopping by starting with formmail.pl, 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.
Figure 6.3 This example of the formmail.pl 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 more quickly compared with 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++.
Figure 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.
Figure 6.5 Hereís a simple HTML interface to the C-based CGI script called mailcgi.
The last 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.
Figure 6.6 This simple HTML form interface is for the AppleScript-based e-mail gateway email.cgi.
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).
Figure 6.7 This is a nicely done example of an HTML interface to a feedback or comments script.
Figure 6.8 This example shows the HTML output of the Feedback script log file.
Figure 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.
You can find various versions of feedback and comment scripts
on the accompanying CD-ROM and at the Web sites listed in table
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.
Figure 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.
Figure 6.11 This section of the guestbook.html log file was created with the Perl CGI script guestbook.pl.
Figure 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).
Figure 6.13 This graphical guest book shows the geographic location of the visitor.
Figure 6.14 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.
You can find these guest book CGI scripts on the accompanying
CD-ROM, so you may want to jump to Appendix A, "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.
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.
Figure 6.15 Notice the graphic numbers at the bottom of this example, which was created by the page-hit counter Count WWWebula.
There are two different ways of incrementing or tripping counters. The first is by causing a CGI counter program to be run every time someone accesses the page. For example, look at the following HTML code:
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 graphic image of the number and sends this graphic to the waiting Web browser.
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.
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 are 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 in which
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. The accompanying CD-ROM includes many of these 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 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-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.
Figure 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).
Figure 6.17 Aquila Internetís home page uses Chris Babbís counter.
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).
Figure 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.
Figure 6.19 Shown here are different examples of various styles of digits for Muhammad A. Muquitís WWW Homepage Access Counter.
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
on the accompanying CD-ROM and at the Web sites listed in table
|Count WWWebula||C/Mac Only||www.io.com:80/~combs/htmls/counter.html|
|Web Page Counter||C/NT Only||www.he.net/~bbsbs/behold/counter.html|
|WWW Counter 1.5||C/NT-OS/2||www.semcor.com/~muquit/Count.html|
|WWW Counter 2.2||C/UNIX||www.semcor.com/~muquit/Count.html|
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. Currently 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.
You have several choices, if you want a commercially available package. You can check out O'Reilly and Associates' WebBoard at http://www.ora.com/. But one of the best publicly available Web BBS software packages is Matt Wright's WWWBoard Version 2.0, which is a set of Perl CGI scripts and HTML forms (see fig. 6.20). 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 http://www.worldwidemart.com/scripts, or on the CD-ROM that accompanies this book.
Figure 6.20 The HTML interface of various WWWBoard-posted messages also shows the replies, names, date, and time of the post.
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).
Figure 6.21 This is the HTML interface of the WWWBoard message-posting area.
Figure 6.22 This section of the WWWBoard message follow-up HTML form allows replies to be posted.
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).
Figure 6.23 Here is a look at the WWWAdmin tools for WWWBoard.
Figure 6.24 Shown here is WWWAdminís HTML interface to remove messages by number.
Figure 6.25 You can use this HTML form in the WWWAdmin area of WWWBoard to access the Change Password HTML form.
For your convenience, the WWWBoard CGI scripts and forms are on the CD-ROM that comes with this book. To keep up-to-date with WWWBoard, the URL of Matt's Script Archive is http://www.worldwidemart.com/scripts/.
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.
Figure 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 action, point your browser to http://www.blueberry.co.uk/gid-bin/cube. But note that this is a heavily hit site, and it could take a while to access.
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.
Figure 6.27 This example of image map techniquesóan Abandoned Missile Base VR Tourócan be found at http:/ /www.xvt.com/ users/kevink/ silo/map.html.
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 (http://www.yahoo.com): Recreation, Games, Internet Games, or Interactive Web Games.