Caching of images

Discussion in 'HTML & Website Design' started by red_fiesta, May 22, 2007.

  1. #1
    What is the best way to stop caching of images in a standard html page??
     
    red_fiesta, May 22, 2007 IP
  2. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can send headers that say not to cache the image, but IE doesn't always pay attention to those, so an easy way is to append a get string to the image name (e.g., "myimage.jpg?5463782"). You can use PHP to generate the string randomly or based on timestamp. Here are two simple examples:

    <img src="snow.jpg<?php echo rand(); ?>" alt="An empty snow field" />
    <img src="snow.jpg<?php echo time(); ?>" alt="An empty snow field" />
    Code (markup):
    Note: This doesn't stop caching, but prevents the browser from using the cached image (i.e., the image will still be stored in the cache, but it will be downloaded again every time the site is refreshed).
     
    Gordaen, May 22, 2007 IP