How can I make sure my CGI-generated page is not cached by the client?

If your CGI-generated page is intended to produce completely different content on each access, it is important to convince the web client not to display a cached copy the next time the user accesses it.

One workaround is to make sure that all links the CGI program generates to itself contain a unique, random piece of information which is then ignored by the program when it arrives as part of the PATH_INFO environment variable. But this is not ideal, since the user will still see the same output again upon returning to a bookmark.

However, consider the following alternatives:

Some browsers support the Pragma: no-cache header. In this case, the following output at the beginning of your CGI program will specify both the content type and the fact that the page should never be cached:

Content-type: image/html
Pragma: no-cache

Note the two carriage returns at the end, always required before the beginning of the actual document.

Alternatively, if the page is "good" for some fixed amount of time, the "Expired:" HTTP header can be used to specify the time after which the page must be fetched again. Important: The Greenwich Mean Time (GMT) must be specified, not the local time.


World Wide Web FAQ