How to prevent the browser from caching the image?

I have one image on my website that I do not want to cache. The image is used as a background in CSS, so I can’t change its name dynamically. Any ideas?

+5
source share
5 answers

Another alternative would be to add a random line after the image file.

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

This should ensure that the image reloads every time a page is displayed.

+8
source

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

, , , , .

+3

apache :

mod_headers:

<FilesMatch "\.(png|jpg|jpeg|jpeg)$">
Header set Expires "Fri, 04 Aug 1978 12:00:00 GMT"
Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

mod_expires:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/png A0
  ExpiresByType image/gif A0
  ExpiresByType image/jpg A0
  ExpiresByType image/jpeg A0
</IfModule>
+3

Apache ( @David Dorvard, - . <Files), PHP () :

<?php 
header('Cache-Control: no-cache');
header('Expires: 0');
header('Content-Type: image/jpeg'); // or whatever your image is
readfile('/some/path/to/yourfile.jpg');
?>

, ; , - (IIRC IE6 , , , ).

Please note that this simplified approach 1) will increase the load on the server, since it needs to run PHP to request an image and 2) disable partial loading on script / image

+2
source

If you are targeting modern browsers, you can use the HTML5 manifest file: http://diveintohtml5.ep.io/offline.html#network

+1
source

All Articles