PHPBuilder - Prevent images from caching

RSS Twitter
Tips Site Operation

Prevent images from caching

by: Dustin Schneider
September 23, 2002

So, you've just made that cool back end database with uploading image support, yet, when the user uploads a new image it isn't shown unless its a full refresh (shift-refresh). Well, I've solved the problem. No, you do not have to set annoying cache headers or any of that other nonsense. The fix is quite easy.

Let's say you have an image:

<img src="someimage.png">

This will be cached on some browsers no matter how many headers you throw at it. So far, this has worked like a charm for me:

<img src="someimage.png?x=<?=uniqid((double)microtime()*1000000,1)?>">

You can of course just pass a random number and such, but as long as you pass a random variable to the graphic file it will force it to reload and no more caching! Hooray!

- Dustin Schneider

Comment and Contribute

Your comment has been submitted and is pending approval.

Dustin Schneider



(Maximum characters: 1200). You have characters left.