Home > Php tricks > How to make a browser reload an image from the server

How to make a browser reload an image from the server

No-cache and no-store tags not working? It happened to me too. I tried all the tricks: the pragma meta tags, the cache-control meta tags, I even made the page have an expiration date from the past. Useless! All of these tactics were useless agains Internet Explorer and Opera. They only worked on Firefox and Safari.

You see, I made this script that updated an image every time someone went to the next page. The image was modified by the php command before the < img src > tag was output but still… no matter what I did, IE and Opera used to run that image from their cache, so the user had to reload the page in order to see the newly generated image. But I managed to get things going.

The solution was quite a simple one. All you need to do is to turn your static url towards the image into an dynamic one, that changes along with the pages, but also display the same image. Since generating a new image everytime some accessed certain pages would be so not a good idea, I did something else.

I converted the <img src=’image.gif’> part into the following one:

$r=rand(1,100);

echo “< img src=’image.gif?”.$r”‘/>”;

The image.gif link will look like image.gif?45 for an example in the current page and image.gif?1 in the following page. Even though they will act just as if the would have been image.gif, the browser will interpret them as different images. So, even though the same image will be displayed, it will be refreshed every time the browser loads a new page.

The advantage of this method is that it also allows you not to use the no-cache tags and there for, the rest of the content will be cached and the page will load faster.

That was it! I hope this will be as helpful for you as it has been for me!

Php tricks ,

  1. No comments yet.
  1. No trackbacks yet.