CGI Programming on the World Wide Web

Previous Chapter 3
Output from the Common Gateway Interface
Next
 

3.2 CGI and Response Headers

By now, you should be reasonably comfortable designing CGI programs that create simple virtual documents, like this one:

#!/usr/local/bin/perl
print "Content-type: text/html", "\n\n";
print "<HTML>", "\n";
print "<HEAD><TITLE>Simple Virtual HTML Document</TITLE></HEAD>", "\n";
print "<BODY>", "\n";
print "<H1>", "Virtual HTML", "</H1>", "<HR>", "\n";
print "Hey look, I just created a virtual (yep, virtual) HTML document!", "\n";
print "</BODY></HTML>", "\n";
exit (0);

Up to this point, we have taken the line that outputs "Content-type" for granted. But this is only one type of header that CGI programs can use. "Content-type" is an HTTP header that contains a MIME content type describing the format of the data that follows. Other headers can describe:

This chapter will discuss how HTTP headers can be used to fine-tune your CGI documents. First, however, Table 3.1 provides a quick listing of all the HTTP headers you might find useful.

Table 3.1: Valid 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).

Table 3.2: 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

Also, there are a couple of things you should know about header syntax:

Header lines don't have to be in any special order.

In general, the headers you generate from a CGI program can be output in any order you like.

The header block has to end with a blank line.

HTTP is a very simple protocol. The way the server knows that you're done with your header information is that it looks for a blank line. Everything before the blank line is taken as header information; everything after the blank line is assumed to be data. In Perl, the blank line is generated by two newline characters (\n\n) that are output after the last line of the header. If you don't include the blank line after the header, the server will assume incorrectly that the entire information stream is an HTTP header, and will generate a server error.


Previous Home Next
Overview Book Index Accept Types and Content Types

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
This HTML Help has been published using the chm2web software.