Uses for CGI
HTML allows you to access resources on the Internet by using other protocols that
are specified in the URL. Examples of such protocols are mailto, ftp, and news. If
you code a link with mailto that is followed by an e-mail address, the link will
result in a generic mail form.
What if you wanted your customers to provide specific information, such as how
often they use the web? Or how they heard about your company? Rather than
using the generic mailto form, you can create a form that asks these questions and
more. You can then use a CGI programto interpret the information, include it in
an e-mail message, and send it to the appropriate person.
You do not need to limit CGI programs to processing searchrequests and e-mail.
You can use them for a wide variety of purposes. Basically, anytime you want to
take input from the reader and generate a response, you can use a CGI program.
The input may even be apparent to the reader. For example, many people want to
know how many other people have visited their home page. You can create a CGI
program that keeps track of the number of requests for your home page. This
program can display the new total each time someone links to your home page.
The CGI process
Usually CGI programs are referred to from within HTML documents. In general,
the HTML document format defines the environment variables that specify the
passing of information. When you design the layout of your HTML document, you
must keep in mind how a CGI program might affect the look of your document.
Developing the CGI program along with the HTML document will help you avoid
many design mistakes.
Overview
The CGI process involves three players: The web browser, the web server, and the
CGI program. To exemplify how CGI programs for online forms work, let us
assume that the web browser has already requested and obtained an HTML form.
1. The user clicks buttons or enters information in fields, and then clicks on an
HTML button to submit the request.
2. The web browser then sends the data to the web server in an encoded format.
For example, the data might consist of responses on an HTML form.
3. When the web server receives data, it converts the data to a format compliant
with the CGI specification for input and sends it to the CGI program.
4. The CGI program then decodes and processes the data.
5. The system sends this response back to the web server in a form that is
compliant with the CGI specification for output.
6. The web server then interprets the response and forwards it to the web
browser.
Note: If a CGI application program must change the HTTP server job attributes
while processing, the CGI program must restore the attributes to their initial
values before the CGI program ends. Failure to restore job attributes that are
changed in the CGI program will result in unpredictable responses to future
server requests. For example, a CGI program that requires a library in the
library list needs to add the library to the library list. The CGI program
must remove the library list before the CGI program ends.
Chapter1. Writing Common Gateway Interface Programs 3