What are headers?

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

RESPONSE TYPE HTTP HEADER
Text Content Type: text/plain
HTML page Content Type: text/html
Gif graphic Content Type: image/gif
Redirection to another Web page Location: http://www.foobar.com
Cookie Set-cookie: ...
Error Message Status: 402

All HTTP headers must be followed by a blank line. Use the following line of code as a template:

print("Content Type: text/html\n\n");

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:

print("Location: $nextPage\n\n");

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.

HTTP Headers

 

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.


You can see a complete list of HTTP headers at http://www.w3.org/hypertext/WWW/Protocols/HTTP/Object_Headers.html

EXAMPLES

#!/usr/local/bin/perl
print "Content-type: text/html", "\n\n";
print "<HTML>";
print "<BODY>";
print "HELLO WORLD!";
print "</BODY>";
print "</HTML>";
exit (0);

To see the result of this example click here