Common Gateway Interface (CGI)
HTML provides us with several useful elements (radio buttons, checkboxes, buttons, text fields, select menus) for collecting a variety of form information with a Web page. When the reader "submits" the form, the information collected is sent to the web server specified in the ACTION attribute of the form element and that server starts the Common Gateway Interface program specified. Common Gateway Interface programs are commonly referred to as CGI scripts.
CGI scripts can be written in a variety of languages including Unix Shell Script, Perl, and C. There are two methods by which form input data can be sent to the Web server for processing: GET and POST.
| GET | POST |
|---|---|
Form data is packaged and appended to the end of the URL. For instance the URL might look like this:
|
Form data is sent separately from the actual call to the script. |
| When the server executes the CGI script, the environment variable QUERY_STRING is set to the value of everything after the question mark. | Unix server receive the form data through standard input. Other servers may store the form data in a temporary file. The environment variable QUERY_STRING is not set. |
| The amount of information which can be stored in the QUERY_STRING could be limited. Information could be truncated. | Since the data is sent as a separate stream, there is no limit to the amount of data that can be sent with the POST method. |
| The GET method has been deprecated in HTML 4.0. | POST is the preferred method. |
URL Encoding is the format used by the browser when packaging up form input data to be sent to the server by either the GET or or POST method (see above).
URL Encoding Process:
- Browser gets all the names (specified by form element NAME attribute) and value (entered or chosen by user).
- Browser encodes them as name/value pairs.
- Each name/value pair is separated by an equal sign (=). Ex: email=tina@mcduffskeep.com
- Name/value pairs are separated by an ampersand (&). Ex: email=tina@mcduffskeep.com&sex=f&married=true
- Spaces in the input are indicated with a plus (+). Ex: name=Tina+McDuffie
- Any special characters (characters that are not simple seven-bit ASCII) are translated.
- Special characters are encoded in hex preceded by a % sign. Ex: ® would be encoded as %AE.
- Any =, &, or % characters in the form input are also encoded.
Because the form input is passed to the CGI script in URL-encoded form, you'll have to decode (parse) it before you can use it. Fortunately there are several free tools available to take care of this onerous task for you. Here are two that we've installed on our Unix box.
- uncgi
- A program written in C by Steven Grimm. uncgi is available at http://www.midwinter.com/~koreth/uncgi.html.
uncgi parses the form data sent and places the information in environment variables with the same name, but suffixed with "WWW_". For instance, if you had a form field named "email", it would be accessible as "WWW_email". Cool, huh? uncgi should be installed in your system's cgi-bin directory. To uncgi form input, call uncgi then append the name of the script that will use the uncgied variables. Ex:
$lt;FORM METHOD=POST ACTION="http://160.227.122.57/cgi-bin/uncgi/myscript.cgi">
- cgi-lib.pl
- A Perl library written by Steve Brenner. cig-lib.pl is available at http://cgi-lib.berkeley.edu/.
cgi-lib.pl is a set of routines for managing form input. cgi-lib.pl should be placed in your system's Perl libraries directory, usually /usr/lib/perl. To access the subroutines from the library in your script, add the following line:
require 'cgi-lib.pl';
References
- CGI Environment Variables
- A list of special variables set in the environment when a CGI script is called. All of these variables are available to you use in your CGI scripts.
- Common Formats and Content-types
- A list of common formats and their corresponding content-type.
- An Instantaneous Introduction to CGI Scripts and HTML Forms
- An excellent description of the Browser-Server interaction between forms and CGI scripts.
Copyright © 1997-2001 by Tina McDuffie

