Prevent Caching Of Dynamic Web Pages

Updated about 8 mths, 24 days ago (July 27, 2023). Know a better answer? Let me know!

Prevent caching of dynamic web pages

This is generally not a problem, because PHP usually generates the headers required to prevent caching automatically. However, should you wish to explicitly generate the headers required to prevent your pages from being cached, here’s how (assuming you’re using PHP and Apache or similar).

Caching is controlled by the HTTP headers sent with every HTTP request. The basic logic is quite simple – if permitted to cache, a cache will store the page for a specified time. This is controlled by the “Cache-Control” and “Expires” headers.

Cache-Control

The “Cache-Control” header instructs modern caches on how they should behave, although it is worth noting that older caches may not obey this field. “Cache-Control” can take a variety of values, such as “private” and “no-cache”. A “public” field in a Cache-Control header indicates that the resource may be cached by any cache. “Private” indicates that the response should only be cached by non-shared caches (such as your local browser), and “no-cache”, rather obviously, indicates that the page or resource being returned must not be cached anywhere.

// tell all caches that they must not cache and revalidate the content.
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1

Expires

The Expires header gives the date and time after which a response is considered stale, that is, after which a cached copy of a page should no longer be considered valid. In other words, the Expires header indicates how long caches should store a cached copy of a page. Here we indicate that pages can be cached for one month from the current date, by specifying their expiry as a date one month in the future.

// this resource expires in the past
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

Not Modified

There’s one further caveat – headers must be sent before any other output. This generally means that headers must be sent before anything else in your PHP code, in other words, that this code must go at the very top of your PHP code before anything else. One way around this is to buffer your page on the server before outputting it, which is done by using ob_start(), which I use to provide gzip compression – further increasing the responsiveness of plain text files by transmitting them compressed, and decreasing the server bandwidth used even more.

All Together Now

Putting all this together gives us:

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

// start output.
// Note that no output can precede the headers unless you call ob_start().
// You don't have to use gzip, but it greatly saves on bandwidth (for text)
// at the cost of a little more processing.
ob_start("ob_gzhandler");

More Information

For more information on:

 

Updated about 8 mths, 24 days ago (July 27, 2023). Know a better answer? Let me know!

Related categories [coloured].

User submitted comments:

Ned, about 9 yrs, 10 mths ago
Sunday June 1, 2014 8:59 AM

This is a test.

Comment on this article (no HTML, max 1200 characters):