-->
Previous Table of Contents Next


Chapter 52
CGI Scripts

by Tim Parker

In This Chapter
•   What is CGI?
•   CGI and HTML
•   CGI and Perl

If you do any work with the World Wide Web, you will come across the term CGI (Common Gateway Interface). While we can’t hope to cover all you need to know about CGI in a chapter, we can look at what CGI is and does, why you want to use it, and how to go about using it.

If you get involved in doing more than simple Web page design (we look at HTML and Java in the next couple of chapters), you will eventually end up using CGI in some manner, usually to provide extra functionality to your Web pages. For that reason and so that you will know just what the term means, we need to look at CGI in a little bit of detail.

What Is CGI?

You now know what CGI stands for—Common Gateway Interface—but that doesn’t help you to understand what CGI does. The name is a little misleading. Essentially, CGI is involved in an application that takes its starting commands from a Web page. For example, you might have a button on your Web page that launches a program to display statistics about how many people have visited your Web site. When the button is clicked, an HTML command starts up a program that does the calculation for you. CGI is involved in the interface between the HTML code and the application, and allows you to send information back and forth between the HTML code and other applications that aren’t necessarily part of the Web page.

CGI does more than that, but it is usually involved in applications that interface between a Web page and a non-Web program. CGI programs don’t even have to be started from a Web page, but they often are because a CGI program has a special set of environment conditions that involve interactions between components that are hard to simulate otherwise.

What does that really mean? When you run a Web page written in HTML, the Web server sets up some environment variables that control how the server operates. These environmental variables are used to control and pass information to programs, as well as many other operations. When a person clicks on a button on your Web page to launch an external application, these environmental variables are used to pass parameters to the program (such as who is starting the application, what time it is, and so on). When the application sends information back to the Web server, that information is passed back through variables.

So, when we talk about CGI programming, we really mean writing programs that involve an interface between HTML and some other program. CGI deals with the interface between the Web server and the application (hence the “interface” in the name).

What’s so exciting about this? In reality, the number of behaviors you can code on a Web page in HTML is somewhat limited. CGI lets you push past those barriers to code just about anything you want and have it interact properly with the Web page. So if you have custom statistics you need to run on your Web page based on a client’s data, you can do it through CGI, with CGI passing the information to the number crunching application, and then passing the results back to HTML for display on the Web page, to take a simple example. In fact, there’s a whole mess of things you can do on even the simplest Web page when you start using CGI, and that is why it is so popular.

The CGI is usually built in to the Web server, although there is no requirement that it exists in all Web servers. Luckily, almost every server on the market (except the very early servers and a few stripped down ones) contain the CGI code. The latest versions of the Web servers from NCSA, Netscape, CERN, and many others all have CGI built in.

CGI and HTML

In order to run a CGI application from a Web page, you make a request to the Web server to run the CGI application. This request is made through a particular method that is responsible for invoking CGI programs. (A method is a procedure or function.) Many methods are built into HTTP (HyperText Transfer Protocol, the protocol used by the World Wide Web), and the method used to call the CGI application depends on the type of information you want to transfer. We’ll come back to methods in a moment after we look at how the CGI code is embedded in the HTML for the Web page.

As the next chapter explains, HTML involves the use of a bunch of tags. To call a CGI program, a tag is used that gives the name of the program and the text that appears on the Web page when the HTML code is executed. For example, the following HTML tag displays the message “Click here to display statistics” on the Web page:


<a href=“crunch_numbers”> Click here to display statistics </a>


Previous Table of Contents Next