|
|||||||||
|
Writing CGI scriptsThe 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 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 ).
Next: Script identification Up: Dynamic Documents Previous: Output from the
Spinning the Web by Andrew Ford |
||||||
Copyright © 1996-2002 Ford & Mason Ltd |