[ Previous ] [ Contents ] [ Index ] [ Next ]

What is CGI and How Does it Work?

CGI (Common Gateway Interface) is a standard way of running programs from a Web server. Often, CGI programs are used to generate pages dynamically or to perform some other action when someone fills out an HTML form and clicks the submit button. AOLserver provides full support for CGI v1.1.

Basically, CGI works like this:

A reader sends a URL that causes the AOLserver to use CGI to run a program. The AOLserver passes input from the reader to the program and output from the program back to the reader. CGI acts as a "gateway" between the AOLserver and the program you write.

The program run by CGI can be any type of executable file on the server platform. For example, you can use C, C++, Perl, Unix shell scripts, Fortran, or any other compiled or interpreted language. You can also use Tcl scripts with CGI, though the extensions to Tcl described in Chapter 5 of the AOLserver Tcl Developer's Guide are not available through CGI.

With AOLserver, you have the option of using the embedded Tcl and C interfaces instead of CGI. Typically, the Tcl and C interfaces provide better performance than CGI. (See the AOLserver Tcl Developer's Guide for information on the Tcl interface and the AOLserver C Developer's Guide for information on the C interface.)

You may want to use CGI for existing, shareware, or freeware programs that use the standard CGI input, output, and environment variables. Since CGI is a standard interface used by many Web servers, there are lots of example programs and function libraries available on the World Wide Web and by ftp. This chapter describes the interface and points you to locations where you can download examples.

Here is a diagram of how a CGI program runs:

For example, suppose you have a form that lets people comment on your Web pages. You want the comments emailed to you and you want to automatically generate a page and send it back to your reader.

  1. The reader fills out your form and clicks the "Submit" button. The <FORM> tag in your page might look like this:

    <FORM METHOD="POST" ACTION="/cgi-bin/myprog">

    The METHOD controls how the information typed into the form is passed to your program. It can be "GET" or "POST" (see page 106). The ACTION determines which program should be run (see page 107).

    Other ways for a reader to run a program are by providing a direct link to the program without allowing the reader to supply any variables through a form, or by using the <ISINDEX> tag (see page 106).

  2. When AOLserver gets a request for a URL that maps to a CGI directory or a CGI file extension (as defined in the configuration file, see page 105), it starts a separate process and runs the program within that process. The AOLserver also sets up a number of environment variable within that process. These environment variables include some standard CGI variables (see page 108), and optionally any variables you define in the configuration file for this type of program.

  3. The program runs. The program can be any type of executable program. For example, you can use C, C++, Perl, Unix shell scripts, or Fortran.

    In this example, the program takes the comments from the form as input (see page 108) and sends them to you as email. If the form method is "GET", it gets the input from an environment variable. If the form method is "POST", it gets the input from standard input. It also assembles a HTML page and sends it to standard output (see page 114).

  4. Any information the program passes to standard output is automatically sent to the AOLserver when the program finishes running.

  5. The server adds any header information needed to identify the output (see page 115) and sends it back to the reader's browser, which displays the output.

Top of Page

[ Previous ] [ Contents ] [ Index ] [ Next ]
Copyright © 1998-99 America Online, Inc.