Forum Moderators: phranque

Message Too Old, No Replies

Code to prevent caching

Does anyone know how to stop the user's Browser from caching the page

         

Demaestro

10:56 pm on Dec 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am looking for a way to have a page not be cached by the users browser. anyone with any insight I would apprciate it.

I was thinking there might be something I can put in the head that will stop the caching of the page.

uncle_bob

11:03 pm on Dec 23, 2003 (gmt 0)

10+ Year Member



I guess the best way would be to use the expires header, either set it from inside the page (if using a scripting language) or from the webserver configuration.

Other than that, I think there are some cache-control/expires meta tags you can add to static html pages, but I'm not sure if all browsers obey these.

Demaestro

11:08 pm on Dec 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes Bob the age old question. When we implement it how many Browsers will comply.

I have been looking and I found that Cache-Control general-header field is used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain. The directives specify behavior intended to prevent caches from adversely interfering with the request or response. These directives typically override the default caching algorithms. Cache directives are unidirectional in that the presence of a directive in a request does not imply that the same directive is to be given in the response.

Note that HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache

There is documentation on it at [w3.org...]

I will be trying this over the holidays and I will let everyone know how it turns out and whast browsers conform to the syntax

Wizcrafts

3:40 am on Dec 24, 2003 (gmt 0)

10+ Year Member



Demaestro;
In case it helps your inquiry, I use these headers in modem baudrate test files that were .html, but which I renamed as .php files after adding php headers.

<?php header("Expires: 0"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("cache-control: no-store, no-cache, must-revalidate"); header("Pragma: no-cache");?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>50 KB Modem baud-rate test </title>
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="expires" content="FRI, 13 APR 1999 01:00:00 GMT">
<META name="ROBOTS" content="NOINDEX, NOFOLLOW, NOARCHIVE">

So far this method has had the desired effect of requiring a fresh page to be requested when the visitor hits refresh, or clicks on my retest link ( onclick=history.go(0) ). Before adding the php expires headers the greater percentage of browsers simply reloaded the page from the browser cache, ignoring the meta tags.

Wiz

Demaestro

4:09 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Nice Wiz thanks

Avitar

5:25 am on Dec 31, 2003 (gmt 0)

10+ Year Member



Demaestro just remeber you can't force browsers to comply... In fact most browsers only half way follow the standards of w3.org to begin with at best. Sad but true.

There is only one way that you can really force most browsers to reload a page, and that is to semi-encrypt a URL at run time. I first had the idea 4 years ago working on an anti-leech program.

For instance if you make a link in CGI like the following with a variable of a page name, and the full date and time as another one (down to the millisecond):

[myDomain.com...]

All you have to do is serve that page via CGI. Make a simple routine that will not load pages that are past the current date and time.

This will force browsers to get a fresh copy, however several browsers will keep their content in their cache. It is more like having the browser get 20 diffrent named versions after 20 visits.

When used in conjunction with the other tags, as noted in this thread... it works even better. Sorry i haven't had time to lear PHP yet, but they are similair as far as i know.

Good Luck

-----
David Smith

[edited by: DaveAtIFG at 5:48 am (utc) on Dec. 31, 2003]
[edit reason] Trimmed sig [/edit]