[CGI] Adapted from the CGI Introduction and Primer documents at http://hoohoo.ncsa.uiuc.edu/cgi/.

What is CGI?

The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers. A plain HTML document that the Web daemon retrieves is static, which means it exists in a constant state: a text file that doesn't change. A CGI program, on the other hand, is executed in real-time, so that it can output dynamic information.

CGI is a mechanism for running programs on a WWW server. Typical applications include accessing a database, submitting an order, or posting messages to a bulletin board.

CGI is a common standard agreed and supported by all major HTTPDs.

HTTP Headers

HTTP is the protocol of the Web, by which Servers and Clients (typically browsers) communicate. An HTTP transaction comprises a Request sent by the Client to the Server, and a Response returned from the Server to the Client.

Every HTTP request and response includes a message header, describing the message. These are processed by the HTTPD, and may often be mostly ignored by CGI applications (but see below).

A message body may also be included:

  1. A HEAD or GET request sends only a header. Any form data is encoded in an HTTP_QUERY_STRING header field, which is available to the CGI program as an environment variable QUERY_STRING.
  2. A POST request sends both header and body. The body typically comprises data entered by a user in a form.
  3. A HEAD request does not expect a body in the response.
  4. A GET or POST request will accept a response with or without a body, according to the header. The body of a response is typically an HTML document.

Most HTTP request headers are passed to the CGI script as environment variables. Some are guaranteed by the CGI spec. Others are server, browser and/or application dependent.

The HTTPD will insert necessary response headers on your behalf, always provided it is configured to do so.

However, it is conventional for servers to insert the Content-type header based on a page's filename, and for CGI scripts it will often be absent or wrong. Hence the usual advice is to print an explicit Content-type header.

GET requests should always be idempotent on the server. This means that whereas one GET request might (rarely) change some state on the Server, two or more identical requests will have no further effect.

In the case of a POST request, form data is passed on STDIN, so the script should read from there (the number of bytes to be read is given by the Content-length header). In the case of GET, the data is passed in the environment variable QUERY_STRING. The content-type (application/x-www-form-urlencoded) is identical for GET and POST requests.

The Common Gateway Interface

How do I get information from the server?

Each time a client requests the URL corresponding to your CGI program, the server will execute it in real-time. The output of your program will go more or less directly to the client.

A common misconception about CGI is that you can send command-line options and arguments to your program, such as

command% myprog -qa blorf

CGI uses the command line for other purposes and thus this is not directly possible. Instead, CGI uses environment variables to send your program its parameters. The two major environment variables you will use for this purpose are:


How do I send my document back to the client?

I have found that the most common error in beginners' CGI programs is not properly formatting the output so the server can understand it.

CGI programs can return a myriad of document types. They can send back an image to the client, and HTML document, a plaintext document, or perhaps even an audio clip. They can also return references to other documents. The client must know what kind of document you're sending it so it can present it accordingly. In order for the client to know this, your CGI program must tell the server what type of document it is returning.

In order to tell the server what kind of document you are sending back, whether it be a full document or a reference to one, CGI requires you to place a short header on your output. This header is ASCII text, consisting of lines separated by either linefeeds or carriage returns (or both) followed by a single blank line. The output body then follows in whatever native format.

CGI Links

CGI Links on YAHOO
CGI Primer (ASCII text)
CGI Interface Specification
CGI Test program (UNIX shell script)
Sample CGI Test program output CGI
Run the CGI Test program on this server

GoBack to the CTEC1906 Page