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 2 of the AOLserver Tcl Developer's Guide are not available through CGI.
The Commercial service level supports only Perl or Unix shell scripts as CGI programs..
With an 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 Chapter 2 for information on the Tcl interface and Chapter 2 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.
- 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 How Web Pages Run CGI Programs). The ACTION
determines which program should be run (see URLs that Run CGI Programs).
- 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 How Web Pages Run CGI Programs).
- When an 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 Configuring CGI with an AOLserver), 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 Standard Environment Variables), and optionally any variables you define in the configuration file for this type of program.
- 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 Input to CGI Programs) 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 Output from CGI Programs).
- Any information the program passes to standard output is automatically sent to the AOLserver when the program finishes running.
- The server adds any header information needed to identify the output (see Header Information Generated by AOLserver) and sends it back to the reader's browser, which displays the output.