-->
Previous Table of Contents Next


When the user clicks there, the program called crunch_numbers is called. (The <a> and </a> HTML tags are anchor tags, which indicate a link to something else. Wherever the tag is positioned in the rest of the HTML code dictates exactly how the page will look on a Web browser.)

As you will see when we look at HTML in the next chapter, you can even use hyperlinks to call a program on another machine by supplying the domain name. For example, the following HTML tag displays the message “Display Statistics” on whatever Web page the code runs on:


<a href=“www.tpci.com/stats.cgi”> Display Statistics </a>

When it is selected by the user, the program stats.cgi on the Web server www.tpci.com is located and run. This might be across the country—it doesn’t matter to either HTML or CGI, as long as the reference can be resolved.

Three kinds of methods are usually used to call a CGI application: the GET, HEAD, and POST methods (all are part of HTTP). They differ slightly in when you use them. We can look at each briefly so you know what they do and when they are used.

A GET method is used when the CGI application is to receive data in an environment variable called QUERY_STRING. The application reads this variable and decodes it, interpreting what it needs to perform its actions. The GET method is usually used when the CGI application has to take information but doesn’t change anything.

The HEAD method is much the same as the GET method, except the server only transmits HTTP headers to the client. Any information in the body of the message is ignored. This can be useful when you need to handle only a user ID, for example.

The POST method is much more flexible and uses stdin (standard input) to receive data. A variable called CONTENT_LENGTH tells the application how much of the data coming into the standard input is important so it knows when all the data has arrived. The POST method was developed to allow changes to the server, but many programmers use POST for almost every task to avoid truncation of URLs that can occur with GET.

A number of environment variables are used by CGI, most of which are covered in much greater detail in CGI programming books. Describing all the variables here without showing you how to use them would be a bit of a waste.

CGI and Perl

If you do get into CGI programming, you will probably find that most of it is done in the Perl programming language (which we looked at in Chapter 28, “Perl”). CGI programming can be done in any language (and many Web page designers like C, C++, or Visual Basic because they are more familiar with those languages), but Perl seems to have become a favorite among UNIX Web programmers.

The reasons for Perl’s popularity are easy to understand when you know the language: It’s powerful, quite simple, and easy to work with. Perl is also portable, which lets you develop CGI programs on one machine and move them without change to another platform.

There are a lot of Perl CGI scripts to be found on the Web. A quick look with a search engine such as AltaVista will usually show hundreds of examples that can be downloaded and studied. For example, one of the most commonly used Perl scripts is called GuestBook. Its role is to allow users of your Web site to sign into a guest book and leave a comment about your Web pages. Usually, the guest book records the user’s name and email address, location (usually a city and state or province), and any comments they want to make. Guest books are a good way to get feedback on your Web pages and also to make them a little more friendly.

When run, the GuestBook CGI program displays a form that the user can fill in and then updates your server’s database for you. A number of versions of GuestBook can be found around the Web, but a sample browser display showing the GuestBook Perl CGI script is shown in Figure 52.1.


Figure 52.1.  A sample GuestBook program sending data to a Perl CGI script requesting information about the user.

Each GuestBook Perl script looks slightly different, but the one shown in Figure 52.1 is typical. The information entered by the user is stored in the server’s database for the administrator to read.

Figure 52.2 shows another Web page with a bunch of sample CGI programs launched from a menu. The selection for the domain name lookup shown in Figure 52.2 results in the CGI application doing a bunch of standard HTTP requests to the server and client, displaying the results shown in Figure 52.3. As you can see, the output shown in Figure 52.3 is in standard font and character size, and there has been no real attempt to produce fancy formatting. This is often adequate for simple CGI applications.


Figure 52.2.  A Web page with some sample CGI applications, a mix of Perl and C, with the domain name CGI sample ready to launch.


Figure 52.3.  The domain name lookup Perl CGI script results in this screen for the author’s machine.

The Perl CGI scripts are not complex affairs. The example (Who Are You?) in the demonstration page shown in Figure 52.3 looks up your information through an HTTP request. The Perl code for this is shown in Figure 52.4, displayed through Netscape. As you can see, there are only a few lines of code involved. Any Perl programmer can write this type of CGI application quickly.


Figure 52.4.  The Perl source code for the Who Are You? application shown in Figure 52.2.

Summary

CGI programming is easy to do, especially with Perl, and adds a great deal of flexibility to your applications. When you feel comfortable writing HTML code and developing your own Web pages (which we can’t explain in this book because of space restrictions), you should try your hand at CGI programming and really add some zing to your Web site. We’ve mentioned some programming languages in this chapter that you may not have worked with or that you want more information about. Here are some useful chapters for you to read:

To learn about the Perl programming language, which is perfect for writing CGI scripts, see Chapter 28, “Perl.”
To learn about using C to write CGI scripts, see Chapter 26, “Programming in C.”
To learn about backing up your system so you don’t lose all the CGI scripts you’ve created, read Chapter 45, “Backups.”


Previous Table of Contents Next