title: How do I prevent caching an image?
body: <p>I have a virtual random image using <code>.htaccess</code> file:</p>
  <pre><code>#   .htaccess
  &lt;Files random.jpg&gt;
      RewriteEngine On
      RewriteRule ^(.+)$ /random-image.php [QSA,L]
  &lt;/Files&gt;
  </code></pre>
  <p>and the following PHP:</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
      //  random-image.php
      $images=glob(&quot;images/*.jpg&quot;);
      $image=imagecreatefromjpeg($images[array_rand($images)]);
      header('Content-Type: image/jpeg');
      imagejpeg($image);
  ?&gt;
  </code></pre>
  <p>and in HTML:</p>
  <pre class="lang-html prettyprint-override"><code>&lt;img src=&quot;random.jpg&quot;&gt;
  </code></pre>
  <p>This works well enough, but the browser has an annoying habit of caching the images, so I always get the same image rather than a new one.</p>
  <p>I know that I can use JavaScript to append a random query string to the end of the <code>src</code> attribute, but that’s not always going to be an option.</p>
  <p>Is there a way I can add additional headers, either in the PHP file or the <code>.htaccess</code> file to tell the browser not to cache the image and to always load from the server?</p>