INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Caching

How can I force a no cache reload of every page? by link9
Posted: 24 Aug 01

This is one of my favorite little scripts that I include on any page that I want to make sure isn't loaded from the cache of a user's browser.  It came from member vasah20 a while back, and it has been requested so many times, I just decided to put it here.

So thanks to vasah20, and without further ado...

<%
Response.Expires = 15
Response.ExpiresAbsolute = Now() - 2
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "private"
%>

On my site, I keep it in a file called nocache.inc on my root and just include it wherever I need it like this:

<!-- #include file="nocache.inc" -->

happy coding everyone!  
Paul Prewett


---------------------
ADDENDUM--------
---------------------
The following was posted by member, ToddWW, and goes into more detail on what the above properties take care of, and also provides a very well thoughtout sure-fire way of ensuring no-cache loads of every page.  It is posted here with his consent.

Thanks, ToddWW.  
--------------


In most cases, with ASP applications, browser cache is our enemy.  

ASP pages are, by nature, dynamic. In contrast, HTML pages are static.  A great deal of work in the Web arena has gone into improving the response time for HTML pages.  Cache was designed to help solve this problem.

Page caching works beautifully for static HTML pages, but doesn't work well at all for ASP pages.  ASP pages often change because the back-end content has changed or the content changes in response to user input.  If the browser displays a cached page, the page may be out of date, or may not respond properly to user input.  The ASP server-side code does not run because the proxy intercepts the request before it reaches your server.  The Response object has several properties that can help solve this problem.  However, there are some problems with these that you should be aware of.


Response.Expires and Response.ExpiresAbsolute
Two good methods with easy properties to use and understand.  However, they don't work all the time.  The problem doesn't lie within the commands themselves--they do add the proper header values to the response; it's that not all browsers act the way you might expect.  Netscape browsers are particularly guilty of this improper behavior.  Futhermore, even if a browser is honoring the content expiration, a proxy server within a corporate network may intercept the request and return a cached page from the proxy server.  Proxy servers don't always honor the expiration date either.  To force the proxy to request a fresh copy of the page, you can use the Response.CacheControl property.


Response.CacheControl
Set this to private to tell the proxy server NOT to cache the page.  Most proxy servers will not cache pages if the CacheControl value is set to private.


Again, unfortunately not all proxy servers honor the CacheControl header either.  With all of the uncertainty around who will and who will not honor response headers in web documents, ASP programmers have evolved a sure-fire method to force ASP page requests to refresh from the application server rather than from the cache or proxy server.  That method involves changing the URL in some unimportant way for each request.

Here is what I do at the top of every ASP page in my application.


<%@ Language=VBScript %>
<% Option Explicit %>
<%
dim uUrl
uUrl = "uUrl=" & Date & Timer
%>


Then anywhere in my document where I am navigating to another page, I use that variable in the URL.  Here are some examples of that.


<%
Response.Redirect "somepage.asp?ID=1422&" & uUrl
%>


<form action="somepage.asp?<%=uUrl%>">


<a href="somepage.asp?ID=1422&Search=find+me&<%=uUrl%>">


Because the date and time change constantly, the above method will append a unique string to the end of every URL reference in your application.  Thus, the browser will be unable to locate it in cache.

The content in the FAQ is based on personal experience as well as many ASP references in my library.  I use all of the methods above and have never had a single problem with caching in my ASP application.

Happy Caching  

ToddWW

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close