1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
<HTML><HEAD>
<TITLE>CGI Interface -- What is CGI and How Does it Work?</TITLE>
<LINK rel=Previous href="cgi-ch.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="cgi-ch2.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="cgi-ch.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="cgi-ch2.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="8847">
</a><h3>What is CGI and How Does it Work?</h3>
<p><a name="8753">
</a>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.</p>
<p><a name="8933">
</a>Basically, CGI works like this: </p>
<dl>
<dt>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.<a name="10586">
</a>
<p></dl>
<p><a name="9467">
</a>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 <a href="tapi-ch.htm#2123">Chapter 5</a> of the <i>AOLserver Tcl Developer's Guide</i> are not available through CGI.</p>
<p><a name="21485">
</a>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 <i>AOLserver Tcl Developer's Guide</i> for information on the Tcl interface and the <i>AOLserver C Developer's Guide</i> for information on the C interface.) </p>
<p><a name="21636">
</a>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.</p>
<p><a name="8957">
</a>Here is a diagram of how a CGI program runs:<br><img src="cgi.gif">
</p>
<p><a name="9226">
</a>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.</p>
<ol>
<li>The reader fills out your form and clicks the "Submit" button. The <FORM> tag in your page might look like this:
<a name="9346">
</a><p>
<dl><dl>
<dt><code><FORM METHOD="POST" ACTION="/cgi-bin/myprog"></code><a name="9347">
</a>
<p></dl>
</dl><dl>
<dt>The <code>METHOD</code> controls how the information typed into the form is passed to your program. It can be "<code>GET</code>" or "<code>POST</code>" (see <a href="cgi-ch3.htm#8716">page 106</a>). The <code>ACTION</code> determines which program should be run (see <a href="cgi-ch3.htm#11484">page 107</a>).<a name="9401">
</a>
<p><dt>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 <code><ISINDEX></code> tag (see <a href="cgi-ch3.htm#8716">page 106</a>).<a name="9439">
</a>
<p></dl>
<li>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 <a href="cgi-ch2.htm#26630">page 105</a>), 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 <a href="cgi-ch4.htm#8698">page 108</a>), and optionally any variables you define in the configuration file for this type of program.
<a name="9348">
</a><p>
<li>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.
<a name="9593">
</a><p>
<dl>
<dt>In this example, the program takes the comments from the form as input (see <a href="cgi-ch4.htm#8692">page 108</a>) 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 <a href="cgi-ch5.htm#8693">page 114</a>).<a name="9279">
</a>
<p></dl>
<li>Any information the program passes to standard output is automatically sent to the AOLserver when the program finishes running.
<a name="9284">
</a><p>
<li>The server adds any header information needed to identify the output (see <a href="cgi-ch5.htm#16483">page 115</a>) and sends it back to the reader's browser, which displays the output.
<a name="9642">
</a><p>
</ol>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="cgi-ch.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="cgi-ch2.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|