SPUG:CGI header question

Simon Wilcox essuu at ourshack.com
Thu Jun 19 11:48:27 CDT 2003


On Thu, 2003-06-19 at 16:52, Peter Darley wrote:
> 	I'm working on a project where I'm storing media (photos, sound files) in a
> database and serving them through a perl script.  My image tags look like
> <img src='Photo.pl?PhotoID=14232'>, which works fine.  What I'm looking for
> advice on is that since this is a perl script browsers wont cache the
> result, so it wants to download the images again weither it is new or not.
> I can't find a way in the CGI module's header stuff to give a date or
> anything so the browser can decide weither it should be loaded from cache or
> from the server.

Hi Peter,

Have a look at the headers the browsers are sending with their request.

There should be one named If-Modified-Since. You can compare this date
with the modified date of your image and return a 304 (Not Modified)
status code instead of 200. This will cause the browser to use the
cached image.

Normally this happens automatically for images in the filesystem but you
have to hand code it into CGI apps.

You could also try setting the 'expires' option in CGI. From the man
page:

print $q->header(-type=>'image/gif',-expires=>'+3d');

That should allow the browser to cache it without an extra request.

HTH,

Simon.




More information about the spug-list mailing list