cache control not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

cache control not working

Post by victor »

I've sent a header to prevent caching of the web page so that it would be refreshed each time the 'back' button is pressed or when the link is clicked in 'back' drop down list in IE

<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header('Cache-Control: no-store,no-cache,private,must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
session_start();
?>

I supposed I've got everything in... but the page still caches..why?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

what makes you think the page is being cached?

it could be the "private" param in the cache controll. that tells the browser to cache the page. try removing that, and maybe the must-revalidate as well.
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

When you key the same URL offline and and have the same page appearing again, isn't that caching? what do u suggest? The page consist of a form with various controls like select and radio buttons. Ideally,the page should be refreshing itself and reverts all controls back to their default values when the user hits the back button after submitting the form.

Tried your suggestion, didn't work. any ideas?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

yes thats definately caching.

which browser(s) ?

im thinking its due to being offline.

maybe in offline viewing mode the browser doesnt listen to no-cache headers?
i think it might make sense for browser to cache most pages,
but only reuse those cached pages if its offline. the average user would prob blame the browser
for being a peice of junk if it did not allow them the ability to view most pages offline,(if they wanted to).

are you on a network which may have its own cache that doesnt follow http spec?

have you configured your browser differently from default for anything you think could be related?

is your server ACTUALLY sending those headers? check.
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

used headers_sent(), returned false, what could be the reason?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

put this at the very top of your script

Code: Select all

ini_set('display_errors', 1);
error_reporting(E_ALL);
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

Full eror reporting has been enabled in the host server php ini files, would have shown errors if present, tried your suggestion and nothing still.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Straight from the php manual...

Code: Select all

<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");
?>
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

I got the following result from webcaching.com:
Date Tue, 14 Dec 2004 00:43:48 GMT
Expires 385 weeks 2 days ago (Sat, 26 Jul 1997 05:00:00 GMT)
Cache-Control post-check=0, must-revalidate, no-store, no-cache, pre-check=0
Last-Modified now (Tue, 14 Dec 2004 00:43:48 GMT) validation returned same object
ETag -
Set-Cookie path=/; phpsessid=81b7c8fcc662ed809d7fafe74a1b91de
Content-Length - (actual size: 8885)
Server Apache

This object has been deliberately marked stale. It has a validator present, but when a conditional request was made with it, the same object was sent anyway. It will be revalidated on every hit, because it has a Cache-Control: no-cache header. It won't be cached at all, because it has a Cache-Control: no-store header. Because of the must-revalidate header, all caches will strictly adhere to any freshness information you set. This object requests that a Cookie be set; this makes it and other pages affected automatically stale; clients must check them upon every request. It doesn't have a Content-Length header present, so it can't be used in a HTTP/1.0 persistent connection. The clock on this Web server appears to be set incorrectly; this can cause problems when calculating freshness.
could it be the problem of the server's clock?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

maybe depending on how the browser responds to conficlting info.
i really doubt it though, theres a lot of webservers w/ incorrect clock settings, and if this were to cause dynamic pages to be cached, i think we would all have serious problems.

i would imagine that a
cache-control: no-cache
header would cause the browser to not cache a page under any circumstance. its a litteral directive, it imo should not be able to be misinterpreted as long as the browser isnt a very old one that doesnt recognize cache-controll headers. even if the expires and last modified headers suggest its ok to cache, if theres that blatant no-cache header, it should listen to it.

again, which browsers have you tried?

have you changed any settings in your browsers?

is this only related to offline viewing?
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

I've tried on the IE ver 6.0.2 so far w/o any proxy server, no setting has been changed in the browser, this is not limited to offline viewing. It happens when the page loads on line and when u hit on the back button aft submit, the page retains its previous selections.

My headers aren't sent at all aft headers_sent() returns false, what could be the cause of that? any ideas?

Gathered from someone that the back button behavior is inevitable in all browsers and little can be done in preventing caching into the temp internet files in IE?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

do you have output buffering on?

sometimes in php.ini there could be a value of say 4096 bytes for output buffering

in this case, headers_sent wont return true until you have sent at least 4096 bytes of regular data

it doesnt mater though, your headers are obviously being sent after looking at the results of that cacheing website.



does the page look exactly the same after submitting and then using the back button?
if it retains all the text in textfields and the like, thats odd(unless you specified thier value in the html itself)

in my experience w/ ie you need to send the cache-control: private header to make ie remember the form values when you use the back button.
by default, it wont.

are you using post, or get for the form method?
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

i've removed Cache-Control:private from the header().

my form uses post and the page looks exactly the same after submitting and then using the back button. There are no text fields on the form except radios and select boxes. all selected values are retained each time the back button is used aft submit.
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

Ok, i see the difference when i call the same page on the local host, seems like the headers are recognised if page is viewed from the local server,i.e

1) The page refreshes itself when the back button is clicked

2) There is nothing found in temp internet folder

but the above charateristics is not repeating itself if the page is called from the remote server. Really in need of some expert advice.

pls help!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i mentioned the private header because normally you MUST send that header or else IE WILL forget the page values when you use the back button. i thought it was odd because in your case ie is remembering w/out that header. in fact, ie usually displays one of its own error pages when you use the back button after submitting a form via post(well, at least ie6 win does).


run this code on your local server, to make sure that your really getting all the headers. maybe your isp is stripping some out or something funky. btw-are you using dialup w/ one of those "speed boosters" ?

Code: Select all

$url = 'http://yourwebsite.com/form_page.php';

$fp = fopen($url, 'r');

print_r($http_response_header); // will print all headers sent from your server

in fact, run that code to fetch the headers of both your internet server, and your localhost(use http://localhost/foo.... for the $url)

see if theres any difference between the headers sent from your 2 servers
Post Reply