| the complete webmaster | ||||
| tutorials | reviews | reference | ||
|
Serving Some Files RandomlyToday I noticed that I have a directory filled with a whole bunch of files that I'd like to use, but I can't make up my mind as to which ones are best. Here's a list of files in that directory: Directory: /home/httpd/html/web LinusTorvals.jpg annette.jpg changls.gif fruit.jpg gates.jpg gates007.jpg off.jpg sly.jpg vase.jpg water.jpg windows.jpgSo, I'll write a small CGI script to randomly choose a file in that directory and display it in a web browser. For example, take a look at the following image and the HTML used::
Take a look at the source code for random1.pl. You'll see that it is quite simple. It gets a list of files in a directory and then randomly selects one. Finally, it uses the Location: header to redirect the client to the file it has selected. One annoying problem related to returning a random image is that your web browser (Netscape, MSIE, etc.) will cache the result. In other words, even if you press the Reload button in your browser, the image displayed will remain the same. The solution to this problem is to use the Expiration HTTP header. This header tells the browser when the content expires. After the expiration date and time, the browser will retrieve the content again. Look at the script random2.pl to see how to set the headers. The following example shows the differences in action:
The following table shows the differences (marked in red) in the headers from the two scripts.
The same script can be applied to send any type of data, including plain text, HTML, java applets, and audio files. Using the Expiration header guarantees that the user will see a fresh file every time that the script is activated. So, has this article useful to you? Have any questions? Let me know at the address below.
Author: Doug Steinwand
More articles about CGI |
| write for us | about us | advertise |
Copyright 1997, 1998 A Big Lime. All rights reserved.