
| The first line of output for most CGI programs must be an
HTTP header that tells the client Web browser what type of output it is
sending back via STDOUT. Only scripts that
are called from a server-side include are exempt from this requirement.
A List of HTTP Headers
All HTTP headers must be followed by a blank line. Use the following line of code as a template:
Notice that the HTTP header is followed by two newline characters. This is very important. It ensures that a blank line will always follow the HTTP header. If you have installed any helper applications for your Browser or are familiar with MIME types, you already recognize the text/plain and text/html parts of the Content Type header. They tell the remote Web browser what type of information you are sending. The two most common MIME types to use are text/plain and text/html. The Location header is used to redirect the client Web browser to another Web page. For example, let's say that your CGI script is designed to randomly choose from among 10 different URLs in order to determine the next Web page to display. Once the new Web page is chosen, your program outputs it like this:
Once the Location header has been printed, nothing else should be printed. That is all the information that the client Web browser needs. Cookies and the Set-cookie: header are discussed in Week 7. The last type of HTTP header is the Status header. This header should be sent when an error arises in your script that your program is not equipped to handle. I feel that this HTTP header should not be used unless you are under severe time pressure to complete a project. You should try to create your own error handling routines that display a full Web page that explains the error that happened and what the user can do to fix or circumvent it. You might include the time, date, type of error, contact names and phone numbers, and any other information that might be useful to the user. Relying on the standard error messages of the Web server and browser will make your Web site less user friendly.
|
|
HEADER |
DESCRIPTION |
|---|---|
|
Content-length |
The length (in bytes) of the output stream. Implies binary data. |
|
Content-type |
The MIME content type of the output stream. |
|
Expires |
Date and time when the document is no longer valid and should be reloaded by the browser. |
|
Location |
Server redirection (cannot be sent as part of a complete header). |
|
Pragma |
Turns document caching on and off. |
|
Status |
Status of the request (cannot be sent as part of a complete header). |
|
The following headers are "understood" only by Netscape-compatible browsers (i.e., Netscape Navigator and Microsoft Internet Explorer). Netscape-Compatible Headers |
|
HEADER |
DESCRIPTION |
|---|---|
| Refresh | Client reloads specified document. |
| Set-Cookie | Client stores specified data. Useful for keeping track of data between requests. |
|
EXAMPLES #!/usr/local/bin/perl To see the result of this example click here |