Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 202,528 times

Contents

Related Categories

Beginning Active Server Pages - Making Content Expire

Making Content Expire

A common problem when creating ASP pages is that although the data usually comes 'live' from the database, the browser often caches that information. Although this can help to reduce the bandwidth usage for your site, it also means that if the user has loaded the page before, he/she could be viewing out of date or inaccurate information.

There are a number of ways around this. First, you can set

Response.Expires = -1

which means that the page will always expire, and the browser will load a new copy. Alternatively, you can set a date on which the page will expire:

Response.ExpiresAbsolute = "5 December 2001"

or, simply the number of seconds in the future:

Response.Expires = 1000 'expires after 1000 seconds

However, in situations such as a shopping cart, even this is not enough. You will find that if the user presses the back button on their browser, more often that not, it doesn't bother re-loading the page... which can cause problems if the user has just completed the order, and then goes back to a full shopping cart. To get around this, there is one last trick... add this code:

'date in the past...
Response.AddHeader "Expires", "Mon, 26 Jul 1997 05:00:00 GMT"
'always modified
Response.AddHeader "Last-Modified", Now & " GMT"
'HTTP/1.1
Response.AddHeader "Cache-Control", "no-cache, must-revalidate"
'HTTP/1.0
Response.AddHeader "Pragma", "no-cache"
'last ditch attempt!
Response.Expires = -1

This code adds a number of fields to the header information sent to the browser, and seems to be effective whatever the browser!

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [1010] Beginning Active Server Pages - absolute beginner!

    Posted by michael poxon on 16 Dec 2006

        Hi,
    When I say I'm an ASP beginner, I mean it! I've only written one trivial bit of code, and got a blank page. I see now that's described in the article snippet below. I did ind...

  • .I want to know how to save the files.whats the extensions...

    Posted by writetoksk on 04 Oct 2006

    hi,


    its first i started ASP.I want to know how to save the files.whats the extensions...Where can i get the sample programs

  • Posted by James Crowley on 28 Dec 2004

    Are you running IIS ? And are you viewing it in your browser via the correct URL? (ie something starting with http:// rather than file:// ) ?

  • Posted by James Crowley on 28 Dec 2004

    It does - you just can't see it ;) We've got an ISAPI filter that rewritse /show/1010/ to something like /show.aspx?id=1010

  • It's not working ;_;

    Posted by HyperHacker on 12 Dec 2004

    Is there something special I have to do besides saving it as a .asp file, or does it just not work on my server? It just spits out the code, even HTML, as plain text.

    [code]
    Let's see if ASP...