Prevent browser page caching

How do you stop a users' browser from caching pages from your site. There is no 100% method of doing this. As a website developer or webmaster, you have to assume for the worst (i.e. that a user is viewing a cached version of your pages) and make sure that you handle it on the next page request.

There are two methods to minimize the chance that a user is viewing a cached version of a page on your site.

  1. Javascript
  2. Response headers (could also be meta tags)

These two approaches can also be used together.

Javascript

Add an onload action that checks to see if this is a cached page or not. If cached version, redirect. The redirect can be implented in various ways. A meta refresh that needs to be canceled by javascript. Change window.location. A simple test to see if a page is cached or not is to include a timestamp in the page.

Problems with the javascript approach

If a user has javascript turned off in their browser, the javascript check will not execute.

Response Headers

These are commands that are returned to before the content of the page. All standards compliant browsers read through the commands which can affect how the page is loaded, stored, ... The following commands will tell the browser that it should not cache the page.
In perl:
print "Cache-Control: no-cache, no-store\n";

In php:
header("Cache-Control: no-cache, no-store");

With meta tags:
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE, NO-STORE">

Problems with the response header approach

Browsers can be configured to ignore these headers. Beware if your webserver is behind a proxy. Some proxies are not configured correctly and will not pass the response headers correctly.

UsableLayout is a website design and development company. We are available for consultation and development of website cache control mechanisms and many other website development services.

Comments

Prevent browser page caching using JS

Hi,

Thanks but could you elaborate on how the JS code is like you did for the Response Headers?

Thanks.

Selvan

Thanks for the information.

Thanks for the information. Was looking for this type of information. Can you add some more information? Thanks.

Thanks for your overview

I'm moving this to critical because in some cases this bug causes the hidden $edit['destination'] value of the login form to be set to 'logout', and to the user it appears they can't login since they are immediately logged out again. Very frustrating.

The other side effect of this browser caching bug are, as stated above, the authenticated user will receive stale pages and if you have the login block enabled it will look as though they're were unexplicably logged out.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options