PHP

Bad image cache in Internet Explorer

Sometimes we need to upload/replace an image with the new one, using the same filename. Oftenly, our browser will display the same image as before we uploaded new image because they’re using the same name and browser just display image from cache. This could be nightmare!

I’ve tried some ways to prevent this bad caching done by the browser. Some only works for some browser and other don’t, mostly in Internet Explorer. Yeah, this browser has bad reputation with caching and I need a way to handle this problem.

Then I came into a conclusion that browser using filename to decide if it need to re-download the file or not. So I need to make a unique filename to call the same image, and it can be done using query string like filename.jpg?foo=bar or just filename.jpg?foo

To make the query string changed all the time, I use time() function in PHP. So to call an image, I will write:
User's avatar
it will generate something like:
User's avatar
and will changed time to time.

Amazingly this little trick solve all my caching problem!

Leave a Reply