Is it true, that if I create an image on the fly using GD, it will not be cached by the browser?

i.e <img class="cipher" src="includes/captcha.php" alt="Cipher" title="" />

I have tested this on a few browsers, and it doesn't seem to cache. But, I see many people adding a random query string to their links, or no-cache headers to the image.

What is the recommended way to stop image caching (for all browsers).

PHP: header - Manual

When using PHP to output an image, it won't be cached by the client.

    The browser neither knows nor cares about where the image comes from. Contrariwise, you have no idea what the browser will really do with the image once it receives it.

    Best you can do is either use an ever-changing URL (which seems tacky) and/or hint in the strongest possible terms that the contents of a given response should not be cached. That's what the various cache-control http headers are for.

      Weedpacket;10947263 wrote:

      Best you can do is either use an ever-changing URL (which seems tacky) and/or hint in the strongest possible terms that the contents of a given response should not be cached. That's what the various cache-control http headers are for.

      :queasy: The PHP: header - Manual must be wrong?

      So is it advisable to add cache control headers to the GD image only, or to the HTML page aswell?

      Some state that the cache headers on images can be ignored, and a changing url should be used also?

        Myth(UK) wrote:

        The PHP: header - Manual must be wrong?

        I don't know about the manual; the question is whether mandor at mandor dot net is wrong.

        So is it advisable to add cache control headers to the GD image only, or to the HTML page aswell?

        They're two separate requests and two separate responses. Use whatever is appropriate.

        Some state that the cache headers on images can be ignored, and a changing url should be used also?

        Any and all headers can be ignored. But those given in Example 2 on the [man]header[/man] page satisfy the requirements of the HTTP spec.

          Weedpacket;10947265 wrote:

          They're two separate requests and two separate responses. Use whatever is appropriate.

          Does anyone ever just answer a question? Instead of being cryptic all the time, which is appropriate? Either? Both? Just the image?

          I am tempted to put the headers on both, and use a query string.

            :bemused: Apologies to the cat.

            The xhtml page that the image was being viewed in was not cached anyway...

            header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
            header('Pragma: no-cache');
            header('Cache-Control: no-cache, must-revalidate');
            header('Content-type: text/html; charset=iso-8859-1');

            But I have added these headers to the GD image also...

            header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
            header('Pragma: no-cache');
            header('Cache-Control: no-cache, must-revalidate');
            header('Content-type: image/png');

            In a little test, using no headers on either the xhtml page or the image. I found that static images were cached, but the php dynamic image wasn't... Just a thought. :rolleyes:

              Okay. It's just that I didn't know whether the page the link was on was supposed to be static or was itself dynamic. The image, since it (effectively) is different every time it's requested, would need to be uncached, but whether the page linking to it did or not depended on whether or not the contents of the page would change.

                Write a Reply...