Other Headers

A CGI script can supply other HTTP headers in addition to Content-Type. In most cases, the server passes these extra headers to the client without executing them. For example, the following Refresh header redirects the client to a new location after a specified amount of time:

Refresh: "5; URL = http://www.deitel.com/newpage.html"

Five seconds after the Web browser receives this header, the browser requests the resource at the specified URL. Alternatively, the Refresh header can omit the URL, in which case it will refresh the current page after the given time has expired.

The CGI specification indicates that certain types of headers output by a CGI script are to be handled by the server, rather than be passed directly to the client. The first of these is the Location header. Like Refresh, Location redirects the client to a new location:

Location: http://www.deitel.com/newpage.html

If used with a relative (or virtual) URL (i.e., Location: /newpage.html), the Location header indicates to the server that the redirection is to be performed on the server side without sending the Location header back to the client. In this case, it appears to the user as if the document rendered in the Web browser was the resource the user requested, when in fact the document rendered is the resource specified in the Location header.

The CGI specification also includes a Status header, which instructs the server to output a specified status header line (such as HTTP/1.1 200 OK). Normally, the server will send the appropriate status line to the client (adding, for example, the 200 OK status line in most cases). However, CGI allows programmers to change the response status. For example, sending a

Status: 204 No Response


header indicates that, although the request was successful, the client should not display a new page in the browser window. This header might be useful if you want to allow users to submit forms without relocating to a new page.

We have now covered the fundamentals of the CGI specification. To review, CGI allows scripts to interact with servers in three basic ways:

  1. through the output of headers and content to the server via standard output
  2. by the server's setting of environment variables (including the URL-encoded QUERY_STRING) whose values are available within the script (via getenv)
  3. through POSTed, URL-encoded data that the server sends to the script's standard input.

Категории