Forum Moderators: coopster

Message Too Old, No Replies

Prevent Image Caching

Online Photo Managment, but image caching is causing issues

         

ryan_b83

7:11 pm on Nov 5, 2006 (gmt 0)

10+ Year Member



Hello, I am trying to put together a basic image managmnet application.

Add/Remove/Modify images for products. However when I do a "modify" in which i upload a new image and replace the older one (with the same name). It still displays the old image unless i refresh the page, at which point it shows the correct and latest uploaded image.

I am assuming this is due to browser caching... is there any way around this?

Thanks,
Ryan

coopster

11:21 pm on Nov 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Is the image being sent in an <img> HTML element or are you sending it out with your own headers? If it is the latter you can send out the appropriate modified headers and the browser should retrieve the newer image.

ryan_b83

4:29 am on Nov 7, 2006 (gmt 0)

10+ Year Member



Hum... not 100% sure what u mean by sending it out with the headers?

its just a simple


<img src='./images/<? echo $result['product_id'];?>.jpg' />

Thanks,
Ryan

adb64

7:52 am on Nov 7, 2006 (gmt 0)

10+ Year Member



You could do something like:

<img src='./images/showimage.php?image=<? echo product_id;?>' />

And then in the showimage.php script you can send your own headers before sending the image data.

adb64

8:33 am on Nov 7, 2006 (gmt 0)

10+ Year Member



For the showimage.php script you could use something like this (not tested):

<?php
/*
some basic checking
*/
if (!isset($_GET['ímage'])) exit;
$ImageFile = $_GET['ímage'].".jpg";
if (!file_exists($ImageFile)) exit;
$Size = filesize($ImageFile);
/*
send out all headers
*/
header("HTTP/1.1 200 OK");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: image/jpg");
header("Content-Length: $Size");
/*
and finally the image itself
*/
readfile($ImageFile);
?>

You could also send a 404 header in case something is wrong at the 2 'exit' statements.

Regards,
Arjan

lewby

12:39 pm on Nov 7, 2006 (gmt 0)

10+ Year Member



Another simple method is to append a time stamp to the filename in the database.

Eg. something.gif becomes something.gif?1162906555

So you save the image as something.gif but when creating the database entry, you append the value of time() to the end. This means that every time you modify the image, the image name will "change" however caching of unmodified images still works.

ryan_b83

9:42 pm on Nov 9, 2006 (gmt 0)

10+ Year Member



I am finding that putting this code in my "display_top();" function before any output work

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");