faqts : Computers : Programming : Languages : PHP : Common Problems : Caching

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 28 people (64%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How do I prevent Netscape 6 from caching generated images? The header settings described in the PHP manual does not seem to work for images.

Jul 18th, 2001 23:40
Martin Seebach, David Bray,


Not sure what you mean by generated images but here is an answer for 
you anyway.

For some reason netscape ignores cache headers and expires headers that 
are attached to an image generated by a servlet.  As a result netscape 
will not go back to the server, for images where the URL is a servlet, 
unless the user presses refresh.

However you can spoof netscape into thinking the image is at a 
different URL each time.  In order to do this you are going to need to 
use javascript  or java in a JSP and generate two random integers.

Attach these random integers to the end of the servlet url as a 
name/value pair as follows

      servletURL?randomInt1=randomInt2

When you reload the page the integers will be two new numbers, netscape 
will think that the URL is different to the URL's of the images it has 
cached and will happily go off to the server and request the new image.

Remember this is a workaround for some unlikeable functionality in 
Netscape Six (I wisk the Punisher would kill all of Netscape it brings 
the web down a notch, and put all of the money into opera) it is not 
necessarily an efficient solution.

David Bray

-----
By generated images I meant those by you described as generated by a 
servlet. 
However, your way to add to the filename is not the simplest one. I use 
something like img.php?t=time() where time() returns the internal time 
of the server. You are sure to get a unique number with each request 
unlike your way where you (theoretically!) could end up getting the 
same combination twice.

Another method I found useful, is that if the dynamic part of your 
image is dependant on a file, you can add the modified time of that 
file to the image name
<img src="img.php?t=<?php echo filemtime($filename) ?>">
and thus only forcing the browser to recache the image if it actually 
changes.

Martin Seebach