Thus far we've seen CGI examples that return virtual documents created on the fly. However, another thing CGI programs can do is to instruct the server to retrieve an existing document and return that document instead. This is known as server redirection.
To perform server redirection, you need to send a Location header to tell the server what document to send. The server will retrieve the specified document from the Web, giving the appearance that the client had not requested your CGI program, but that document (see Figure 3.1).
A common use for this feature is to return a generic document that contains static information. For example, say you have a form for users to fill out, and you want to display a thank-you message after someone completes the form. You can have the CGI program create and display the message each time it is called. But a more efficient way would be for the program to send instructions to the server to redirect and retrieve a file that contains a generic thank-you message.
Suppose you have an HTML file (thanks.html) like the one below, that you want to display after the user fills out one of your forms:
<HTML> <HEAD><TITLE>Thank You!</TITLE></HEAD> <BODY> <H1>Thank You!</H1> <HR> Thank You for filling out this form. We will be using your input to improve our products. Thanks again, WWW Software, Inc. </BODY> </HTML>
You could use the programs discussed earlier to return static documents, but it would be counterproductive to do it in that manner. Instead, it is much quicker and simpler to do the following:
#!/usr/local/bin/perl print "Location: /thanks.html", "\n\n"; exit (0);
The server will return the HTML file thanks.html located in the document root directory. You don't have to worry about returning the MIME content type for the document; it is taken care of by the server. An important thing to note is that you cannot return any content type headers when you are using server redirection.
You can use server redirection to your advantage and design CGI applications like the following:
#!/usr/local/bin/perl $uptime = `/usr/ucb/uptime`; ($load_average) = ($uptime =~ /average: ([^,]*)/); $load_limit = 10.0; $simple_document = "/simple.html"; $complex_document = "/complex.html"; if ($load_average >= $load_limit) { print "Location: $simple_document", "\n\n"; } else { print "Location: $complex_document", "\n\n"; } exit (0);
This program checks the load average of the host system with the uptime command (see Chapter 3, Output from the Common Gateway Interface for an explanation of the regular expression). Depending on the load average, one of two documents is returned; a rich, complicated HTML document with graphics if the system is not "busy," or a simple text-only document otherwise.
And the last thing to note is that you are not limited to returning documents on your own server. You can also return a document (static or virtual) located elsewhere on the Internet, so long as it has a valid URL:
print "Location: http://www.ora.com", "\n\n";
For example, this statement will return the home page for O'Reilly and Associates.