Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent browsers from caching an image? [duplicate]

Tags:

html

css

php

I have one image on my website that I don't want cached. The image is used as a background in CSS so I can not change it's name dynamically. Any ideas ?

like image 935
Alex Avatar asked Dec 04 '22 08:12

Alex


2 Answers

Another alternative would be to add a random string after your image file.

<img src="/path/to/image/image.jpg?<?php echo time(); ?>/>

This should ensure that the image is reloaded each time the page is displayed.

like image 113
Garry Avatar answered Dec 25 '22 20:12

Garry


mnot has a good caching tutorial that will explain how to set the HTTP headers to request that the image is not cached (remember, you need to set the HTTP headers for the image, not the HTML document).

This is probably a bad idea though as images tend to be relatively chunky, so redownloading it for every page could impose a significant performance slow down.

like image 43
Quentin Avatar answered Dec 25 '22 22:12

Quentin