ARTLUNG LAB Share

Created 12/2001

Preventing Browser Cache

Multiple Platform Solutions

Bill Volk wrote on the WebSanDiego mailing list:
> On a similar subject ... what's the best way to force a Browser to do a
> refresh on a page when the user uses the "Back Arrow" to get to it? I need
> to referesh the session objects.

This is a persistent problem, particularly with sites whose content updates frequently. The most common answer I see answering this question is to use <meta> tags in the <head> of the document.


<meta http-equiv="Expires" content="Tue, 01 Jan 2000 12:12:12 GMT">
<meta http-equiv="Pragma" content="no-cache">

...although this works inconsistently or not at all in Internet Explorer.

The way to do it according to the HTTP spec (RFC 2187) is to generate raw HTTP headers. Below are some different code snippets for some different server-side languages.

In ASP/IIS:
  http://support.microsoft.com/support/kb/articles/Q234/0/67.asp
  <% Response.CacheControl = "no-cache" %>
  <% Response.AddHeader "Pragma", "no-cache" %>
  <% Response.Expires = -1 %>

In PHP:
  http://www.php.net/manual/en/function.header.php
  <?
  Header('Cache-Control: no-cache');
  Header('Pragma: no-cache');
  ?>

In COLD FUSION:
  <cfheader name="Expires" value="#Now()#">
  <cfheader name="Pragma" value="no-cache">

In JSP:
  http://www.jguru.com/faq/view.jsp?EID=377&page=2
  <%
  response.setHeader("Cache-Control","no-cache");
  response.setHeader("Pragma","no-cache");
  response.setDateHeader ("Expires", 0);
  %>
If you want to view the raw headers of
your web page - you can use this service:
http://www.delorie.com/web/headers.html

Sample HTTP Headers: (for this page)
HTTP/1.1 200 OK
Date: Tue, 29 Oct 2002 19:50:44 GMT
Server: Apache/1.3.27
X-Powered-By: PHP/4.2.2
Cache-Control: no-cache
Pragma: no-cache
Connection: close
Content-Type: text/html