Disabling browser caching

03 January 2010

There are two basic ways of disabling browsers to cache your web page. You can add related meta tags inside the head part of html or add response headers to page at the server side if you are using server side programming languages like PHP, Java/JSP or ASP.NET.

1. Disabling browser caching inside HTML

<meta http-equiv=”Pragma” content=”No-Cache” />
<meta http-equiv=”cache-control” content=”no-cache, no store” />
<meta name=”Expires” content=”Mon, 26 Jul 1997 05:00:00 GMT” />

Add the code above inside <head></head> tags of your page. The main disadvantage of this technique is that you can’t use inside non-html pages like XML or RSS.

2. Disabling the browser from caching at the server side

To prevent caching you have to add some headers to response. You can find code samples for PHP, ASP.NET and Java/JSP above. You can add these header to any kind of pages / files.

PHP

 header("Expires: Mon, 01 Jul 1990 05:00:00 GMT");
 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 header("Cache-Control: no-store, no-cache, must-revalidate");
 header("Cache-Control: post-check=0, pre-check=0", false);
 header("Pragma: no-cache");

ASP.NET

Response.ClearHeaders();
Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0
Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.0

I found this ASP.NET code at Stackoverflow: Making Sure a Web Page is not Cached Across All Browsers. I hope it works (:

Java

response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //proxy level cache disabling

Vote this post

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...
Categories: Coding
Tags: , , ,

Comments

Leave a Reply