problem with image refresh (possible solutions included)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

problem with image refresh (possible solutions included)

Post by vasilis »

I am running Apache 1.3.26 and php 4.0.6 in a WinME system, using it for building my php apps. Recently ive been working on a online catalogue e-commerce site which among others has an administration interface through which the administrator can update his products characteristics, including the photos (by uploading the images to the server). When the image file is updated in the mysql database, the administrator can preview the updated product by running a php script which retrieves its charactheristics from the database. My problem is that instead of the updated image, the previous image shows up in the page (the one that is retrieved from the cache) and in order to preview the updated image, the administrator has to refresh the page.
I have tried various alternatives in order to refresh the page programmatically or disable the caching, but none of these work (at least, in my system):
a) by sending a http header to disable the caching (one of the following lines)

Code: Select all

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
                                                      // always modified
header ("Cache-Control: no-cache, must-revalidate");  
header ("Pragma: no-cache");
(got these from the php manual). Here, the browser still gets the image from the cache.

b) by using javascript to refresh the page programmatically:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
<!--
location.href="admin_preview_prd_type.php?<?=$QUERY_STRING?>";
-->
</SCRIPT>
Here, the browser takes forever to output the page

c) by using meta tags with "refresh" attribute, but the browser gets stuck in an endless loop because the page refreshes indefinitely.

d) by using the javascript

Code: Select all

reload(true)
method but nothing happens.

Finally, I just used a prompt for the administrator that tells him/her to refresh the page, but I would like to end up with a better "coding" solution.
Would you think that one of these methods would work in a real Apache server or is there another solution ? I appreciate any help, Vasilis
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

This may not be what you are after, but I generally attach a time stamp to the front of the image name and update the image call throughout the system.

The new image will help avoid these caching issues. Plus inadvertantly overwriting an image has been an issue with some users in the past, so this will take care of that also.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

you can suppend a random string/numeric to the .jpg call - this though means the image is never cached for anyone ever

img src="image.jpg?<?php echo rand(0,99999); ?>" />

the ?75845 (or whatever) tricks the browser into always thinking of it as a new image - the server just ignores the ?numeric bit

for your problem though, you might prefer to add a 'version number' column to the database and prepend that to the filename eg v4_image.jpg - then just update the version for each renewal and
echo $row['version_number']. '_' .$row['image_ref']
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

thanks for your replies. The trick with the random number appending did work ok. I am thinking of maybe using the 2nd solution with the version number that is stored in a separate column. I guess this method doesnt affect the caching, or does it?
Post Reply