Ford & Mason Ltd
HOME
ABOUT
CONTACT
RESOURCES
ADVERTISING



donations support the development of
cronolog.org
and
refcards.com

next up previous contents index
Next: Script identification Up: Dynamic Documents Previous: Output from the


Writing CGI scripts

The CGI program extracts information from its command line, the standard input stream and environment variables set up by the server, and prints its output to the standard output stream. The data output can be in any format, but the format should be defined by a Content-Type header: the most common types of output are HTML documents, where the content type should be text/html, and plain text files (text/plain).

The program can be written in any programming language, compiled or interpreted, supported on the server system. CGI scripts are quite commonly written in an interpreted language, especially Perl. In fact there are a lot of useful CGI scripts and server tools written in Perl, distributed on the Net. Perl is very well suited to this type of thing, due to its string-handling and pattern-matching facilities and its support of associative arrays. The following example shows a very minimal CGI script written as a Bourne shell program. This program generates an HTML document giving the time and date as returned by the server system's date command:

    #!/bin/sh

echo "Content-Type: text/html" echo echo "<HEAD><TITLE>Date and time</TITLE></HEAD>" echo "<BODY>" echo "The current time is on this system is " date echo "</BODY>"

CGI scripts are frequently written as wrappers for existing applications: decoding information from the CGI inputs, passing it to the application in a suitable format, and transforming the application's output into an HTML document, inserting the appropriate headers. If the information generated is volatile, an Expires header should be included, to inform the browser and any intervening caching proxy servers that the information should not be cached.

Servers are sometimes configured to pass the output from scripts back to the browser without any parsing. In this case the script is responsible for generating a complete, valid HTTP response, including a status line (see Section gif).



next up previous contents index
Next: Script identification Up: Dynamic Documents Previous: Output from the

[ITCP]Spinning the Web by Andrew Ford
© 1995 International Thomson Publishing
© 2002 Andrew Ford and Ford & Mason Ltd
Note: this HTML document was generated in December 1994 directly from the LaTeX source files using LaTeX2HTML. It was formatted into our standard page layout using the Template Toolkit. The document is mainly of historical interest as obviously many of the sites mentioned have long since disappeared.

 
Copyright © 1996-2002 Ford & Mason Ltd