pickledegg has asked for the wisdom of the Perl Monks concerning the following question:

Maybe this is not strictly a pure perl question, as it involves HTML too, so don't hurt me

I am calling a cgi script using the img tag method. However as the browser caches the image, it is not calling all the time. Does anyone know a way of stopping the browser caching the image?
I've tried a meta tag and I know that there is the no cache pragma with perl, but including this with the return image is 'shutting the gate after the horse as bolted', as obviously if the browser has already cached it wont call the script.

The requesting page is not cgi generated, so it looks like I'll have to rely on html tags?

Replies are listed 'Best First'.
Re: Calling CGI but not caching
by dorward (Curate) on May 15, 2006 at 11:25 UTC

    Caching isn't the only reason the image may be cached not be loaded, the browser might have images disabled, or not support them in the first place.

    A meta tag isn't going to do you any good, nor is changing the HTML document - it is the image being cached, not the HTML.

    You need to send real HTTP headers along with the image, and you can do that from CGI. The standard text on the subject is the mnot Caching Tutorial.

    As for images already cached by the browser - you'll just have to wait until their cache expires and they request it again (and get the new cache control headers). That, or hack around it by changing the URL of the image in the HTML source (adding/changing a query string is a simple way to do that).

Re: Calling CGI but not caching
by jonadab (Parson) on May 15, 2006 at 13:30 UTC

    I think that Apache (I assume you're using Apache; in this kind of context if your web server is anything else you should say so) by default will send Pragma: no-cache along with the output of a CGI script, but if someone has changed the value of CacheNegotiatedDocs in the configuration, this may not be happening. In that case, you could work around it by printing out that header yourself.

    If you aren't sure whether the pragma header is being sent, you could use WWW::Mechanize to find out.

    If the pragma is being sent and the web browser is ignoring it, I'm not sure what to suggest other than adding a random element to the query string to make the url unique each time, but that would require that the HTML document be generated each time, so it won't help you if the image is designed to be used from static HTML documents (e.g., if it's a counter).


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.
Re: Calling CGI but not caching
by ahmad (Hermit) on May 15, 2006 at 12:50 UTC

    Hello ,

    Add the following lines before </head> tag

    <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="-1">

    Hope it works

      The OP is generating an image, which doesn't have an HTML head section.