|
|
Keeping a web site up-to-date is a very time consuming task. Everybody wants to have a web site that looks cutting edge but may not have the time or resources available to continuously update many parts of a web site. Of course, the most important part of a web site is the main page, the one that comes up with someone enters http://www.yoursite.com. It is your first opportunity to make a good impression.
One cool way to look up-to-date is to include the current date on the web page itself. You can do that in many different ways. This week, I'll demonstrate how a small Perl script can display a .gif image with the name of the current month in it. To start out, download the following:
First, unzip the source file and move the monthimage.pl script to your cgi-bin directory. Make sure that it's readable and executable (chmod a+rx monthimage.pl). Next, move the images (*.gif) to a convenient directory. Then, edit monthimage.pl and change $IMAGEPATH to point to the directory where you put the images. In other words, if you put the images in /home/my/images, then use the following:$IMAGEPATH="/home/my/images"
You'll notice that this month's name is prominently displayed at the top of this page. If you come back next month, you'll notice that it will automatically change. In other words, nobody has to take the time to update this html page... the change happens automatically.
To do this, the image itself is actually a cgi program, and the HTML source for it looks something like this:
<img src="/cgi-bin/monthimage.pl">
The script monthimage.pl calculates the current month and returns the contents of a .gif image. There are 12 .gif images, named 1.gif, 2.gif, ..., 12.gif. So, 1.gif has "January", 2.gif has "February", etc. The script also returns headers to describe when the image will expire. This way, the web browser shouldn't need to retrieve the image every time it displays the page. It also guarantees that the image will expire at the end of the month, so when next month rolls around, the browser will then retrieve the correct image.
The output of the monthimage.pl script looks something like the following:
Content-type: image/gif
Expires: Thu, 01 Jan 1998 00:00:00 GMT
Date: Tue, 02 Dec 1997 01:20:00 GMT
GIF87a image content starts here
This is a quick and simple way to keep your web site up-to-date. There are many enhancements that can be made to the script. For example, it could easily support other MIME types, such as Jpeg. There are also other approaches that could be used, such as server-side includes and Javascript.
Note: This program has been tested on many flavors of Unix running the Apache web server. It may not, however, work correctly on all web servers, especially non-Unix ones.