View Full Version : Can a web page tell a browser to perform a hard refresh?

02-13-2012, 05:27 AM
Each week I upload GIF files into a folder on my ftp site. There may be 10 of them, and they are always named exactly the same as the previous week, therefore they overwrite the old ones.

More often than not, I need to refresh the browser for the updated images to show. I can't expect my site visitors to understand that they need to perform a hard refresh.

Is there any html code that I can place in my page that will tell a revisiting browser that it should perform a hard refresh? My first instinct is that this would really be controlled by the individual in their browser settings, and my answer is no. I just want to ask to make sure.



02-13-2012, 05:43 AM
Is there any html code that I can place in my page that will tell a revisiting browser that it should perform a hard refresh?Presumably, this problem could be ameliorated by sending out a Last-Modified HTTP header (http://tools.ietf.org/html/rfc2616#section-14.29) with the images. I'm not quite sure how you do that though.

02-13-2012, 05:56 AM
Hi Arbitrator,

Thanks for the reply. I'm going to read up on what you mentioned. The images are really GIF files of my newspaper pages. By reusing the file names each week, I don't have to open the actual web page and perform any coding. It's simply drag and drop. It's slick, but it could be hours before a revisiting browser displays on its own my updated content/images.



02-13-2012, 05:56 AM
Slightly different from what you asked, and possibly wrong, but might generate some interesting googling. I thought I remembered someone solving this problem via something like:

<img src='myimage.gif?id=12345'>

Where the ID was a randomly generated number (ie, probably created in PHP or perhaps JS?), which caused the URI to be different on each browser load and therefore not found in the cache. So you end up with no cache, rather than a weekly cache, though you might add the smarts to it like:

<img src='myimage.gif?id=ww12'>

and only change the id weekly.

I've never fiddled with this, but it might be worth a try if you have the time.


02-13-2012, 06:05 AM
The way tracknut suggested is the way I do it. I just have php echo out a random string of numbers

<img src="image.gif?<?php echo rand(1,999999999); ?>" alt=""/>

02-13-2012, 06:21 AM
Thank you Dave and Nightfire,

Okay, this is great. So what I need to do is open the code and instead of my standard image tags, implement what Nighfire has shown? Is this all there is to it, or do I need an additional php script?



02-13-2012, 08:34 AM
Is this all there is to it, or do I need an additional php script?

That's all you'd need to force a reload each and every time the image is referenced.

If you just want to force the reload once a week then work out what offset you need to swap the change of week number on the server to alighn with when you upload the new images and just update the current date/time by that offset and then grab the week number and add that instead of the random number.

The other alternative as Arbitrator mentioned is to set a Last_modified header. The easiest way to do that would be to serve the images via a PHP script that reads in the image and sends it to the browser after adding the appropriate headers. You'd then reverse the src around on the img tag so it calls the PHP script passing the image as the querystring.

The following code posted in the PHP manual shows how to send headers from PHP to identify if the given file has been modified since the cached copy.

$last_modified_time = filemtime($file);
$etag = md5_file($file);

header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: $etag");

if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header("HTTP/1.1 304 Not Modified");

You'd then need one more header to identify the file as a gif image:

header('Content-Type: image/gif');

and code to transfer the content of the actual image (note you should also validate the $_GET value is one of your images before running any of the code.