Forum Moderators: open

Message Too Old, No Replies

Forcing Browsers not to Cache Pages

How to force browser reload of pages

         

Nick_W

8:36 pm on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I'm having some trouble with browsers caching pages. I don't want them too ;)

The problem is that when one of my dynamic pages gives 'one view' of a page but then I do some stuff and return to the same page, the old 'one view' is still there unless I hit 'reload'.

I'd like it to show the new information of course (without manually hitting reload).

I program PHP so have included these headers at the top of every page:


header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

But the problem persists. Anyone explain why that is and how the problem might be addressed?

Many thanks

Nick

aspdaddy

8:40 pm on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont think theres any 100% way of doing it , I use this which looks similar to what you got.

' stop page caching on the client
Response.Expires = 0
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"

SethCall

10:48 pm on Mar 21, 2003 (gmt 0)

10+ Year Member



well just checking.. but does your php script have *any* thing before the headers?

like... if u even have a blank line before
<?php
header blah blah

you can have a problem. and of course, u cant have docttypes, <head> or anything else b4 your first php line :)

jdMorgan

10:56 pm on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This works for Apache servers in .htaccess:

# Set up Cache Control headers
ExpiresActive On
<Files example.html>
ExpiresDefault A1
Header unset Cache-Control:
Header append Cache-Control: "no-cache, must-revalidate"
</Files>

Jim

DLadybug

11:16 pm on Mar 21, 2003 (gmt 0)

10+ Year Member



What's really sad is you can't control it at all if your visitor is accessing via AOL. It seems they have a cache that they serve from.

I have found deleted graphics files being displayed by aol browser for up to 2 hours, no matter what I did about clearing my own cache. Everyone told me I was nuts, that i wasn't clearing my cache properly, etc.

[webmaster.info.aol.com...]

When using a browser integrated with the AOL client software, AOL members make web requests through a set of caching proxy servers. If the servers have a current copy of a requested web object, it will be served to the member directly from the cache server instead of the request going over the Internet to the origin web server. AOL will cache most types of web objects including JavaScript files, HTML, and images.

that's just AOL, but I'd suspect many other web gateways do much the same thing.

Dian

Nick_W

7:19 am on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No Seth, nothing before the headers (come on, I'm not that green ;)).

Jim, would this work for all PHP files?
<Files *.php>

Also, just to explain further, in case it's important. This is a typical situation:

Page checks if user has certain cookies.
If he does, it shows the 'You're already logged in" msg.
If he doesn't, it shows the login form.

I appear to be having issues with both cookies being set, and, when they are set/unset - the correct msg being shown.

The different msg's are all variables passed to a template system.

The only way I've found round this (and its not really appropriate on all pages) is to have the page refresh itself using the PHP header() command. This seems to force the browser to reload.

It's a 'kludgy' solution at best though....

Thanks

Nick

DrDoc

1:52 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nick,

Yes, <Files *.php> will work for all PHP files.

Do you know which browser causes the problem? That might help in answering your question :)

jdMorgan

2:30 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dian,

If the [AOL] servers have a current copy of a requested web object, it will be served

...which is precisely why we need to control the cacheability [mnot.net] of our pages, images, scripts, etc. by specifying when these objects cease to be "current."

Using the snippet of .htaccess code I provided above, plus more, I control the expiry time of all objects on my sites, and after thorough testing [ircache.net], I have no trouble with AOL or any other caching proxies of any importance. There may be a few services out there which disregard or mishandle HTTP headers related to cache control, but AOL is not one of them.

Nick,
Yes, as DrDoc answered, "*.php" should work fine. See the Apache core documentation on <Files> for details.

HTH,
Jim

Nick_W

2:45 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DrDoc,

Opera.

No matter what I do, I just can't get it to either NOT cache a page or to (on occasion) to even refresh a page to a new location.

All I'm doing on the page (chronalogically) is this:

  • Send the no-cache headers in my earlier post
  • unset some cookies (by assigning dates in the past)
  • Refresh to another page (where the pages value should show cookies as unset)

Here's what happens.

When Opera loads the page that unsets the cookies and refreshes to the 'login' page the refresh 9/10 works but, you have to hit reload for the cookies to unset. - If you do hit reload, it will unset the cookies and refresh to the 'login' page.

On the login page, it still says 'welcome Nick W' - NOT 'welcome Guest' as it should. (same no cache headers are sent with this page). As there is no 'uname' cookie it and all the no cache headers are sent with this page it should not know what Nick W is right?

Now, Brett doesn't seem to have any trouble here on WebmasterWorld, I logged in and out a few times with no problem whatsoever.

So, i'm lost. I've examined everything and cannot pinpoint the area that's causing the trouble.

I've even put the no-cache stuff in the <head> of the html and copy'n'pasted the the exacts <meta> tags that Opera recoomend on their site.

It works exactly as intended in Moz, I've not tried Win IE as it's only a development site on my home machine and not on the www at the moment...

Nick

DrDoc

3:08 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I figured it was Opera (but I didn't want to look stupid if it weren't ;) )

Hmm.. tricky. Do you make sure to assign a value to the cookie when it's deleted? I've had problems with Opera in the past if I pass an "empty" cookie to it ..

No clue about the refresh thing. Are you using JavaScript cookies, or PHP/Perl?

Nick_W

3:16 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the code:

[pre]
<?
$time=60*60*24*$DEFAULT_COOKIE_TIME;
setcookie("id", "", time()-$time, "/", ".site.org");
setcookie("uname", "", time()-$time, "/", ".site.org");
setcookie("logged_in", "", time()-$time, "/", ".site.org");
header("Refresh: 3;URL=index.php");
?>
<html>
<head>
<title>Your Browser will Not Refresh</title>
</head>
<body>
<h1>Your Browser will Now Refresh</h1>
<p>Please <a href="/memberadmin/">click here.</a> if it
doesn't.</p>
</body>
</html>[/pre]

The Refresh header is just cos I wanted to see if it made a difference with a delay. (works same with 'Location: ').

The capitialized variables are the defauts the cookies are set with. eg $DEFAULT_COOKIE_TIME=150 (6mts).

Guess the values are not being set?

Nick

DrDoc

3:21 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That sets three cookies, right?
Aren't you technically allowed just one per domain?

Nick_W

3:23 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As many as you like I think...

unsets 3 cookies, allegedly ;)

Nick

DrDoc

3:31 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



unsets 3 cookies, allegedly ;)

yeah, yeah... ;)

And, they work when they are set? If you change values, the change is reflected immediately?

Nick_W

4:41 pm on Mar 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, it's definately not the cookies.

The cache is just being very weird....

I just can't seem to make opera not cache a pages...

Nick