eekim.com > Talks > CGI Programming slides (1996)

Input    (01)

Input is slightly more complex than output. There are two ways that a CGI program can retrieve input. The first is environment variables. Certain useful information is always passed via environment variables, such as the remote host name. The second is through the standard input.    (02)

When all the information (including submitted form input) is sent via environment variables, this is called the GET method. The GET method stores the input in the environment variable QUERY_STRING. When the browser sends form information via the GET method, it simply appends this information to the URL. For example, when you use a search engine like Alta Vista, you may see a URL like:    (03)


http://www.search.com/cgi-bin/search.cgi?keyword=cows    (04)

The information following the question mark in the URL is the submitted form data.    (05)

Because of how the GET method sends and stores the input, there is a restriction on the size of the input. A better method is needed to send large chunks of information. The POST method serves this role. The POST method stores the length of the information it is sending in the environment variable CONTENT_LENGTH, and it sends the information itself to the standard input.    (06)

CGI Encoding    (07)

Think about the kind of information you are sending when you fill out an HTML form. Each form field needs to have some identifying attribute, a name. Whatever you type into that field is the field's value. So when you submit the form, the browser needs to send a field's name and its associated value.    (08)

The proper way to encode this information for CGI programs is to separate each name and value with an equal sign, and each name/value pair with an ampersand. For instance, if you have two form fields - a name and age text field - the submitted data may be encoded like this:    (09)


name=Eugene&age=21    (010)

There are two fields here - name and age - and the user typed "Eugene" for name and "21" for age.    (011)

Additionally, certain characters must be encoded before submission. For example, both the equal sign and ampersand need to be encoded since they both have special meaning in this case. To encode a special character, you use a percent sign followed by the two digit hexadecimal ASCII code. For a complete list of characters that need encoding, see the CGI specification.    (012)

Useful Environment Variables    (013)

QUERY_STRING Stores the input string when using the GET method. Will always return the value appended to the URL regardless of what method is used.
REMOTE_HOST The remote hostname of the browser connected to the server.
HTTP_USER_AGENT The browser name, version, and usually its platform.
HTTP_ACCEPT A comma-delimited list of the MIME types the browser is capable of interpreting.

Next | Table of Contents | Previous    (014)