Forum Moderators: coopster

Message Too Old, No Replies

Preventing caching in PHP (back button)

Preventing caching in PHP - forcing the back-button to re-load a page

         

DrexlSpivey

4:03 pm on May 23, 2007 (gmt 0)

10+ Year Member



Hi,

can somebody please help:

I am trying to force the back-button to re-load a page in IE, but even the following basic script, based on [uk.php.net...] is not working:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$timenow = time();
echo date("r",$timenow);
?>

I'm using php 4.3.4

(Note that the ASP script at [support.microsoft.com...] does work, but I haven't been able to successfully combine it with my existing php code).

Am I missing something very basic here? Any help appreciated!

Thanks in advance.

eelixduppy

8:39 pm on May 23, 2007 (gmt 0)



Welcome to WebmasterWorld!

Are you getting any errors either output to the browser or in your error logs? It could be that the headers aren't being sent properly to the browser because a text character was sent before the call to the function. Make sure you have no text, whitespace, etc... before the header() function otherwise it won't work.

One last thing. Is this happening only with Internet Explorer or it an issue all around?

Hopefully these suggestions will lead to your answer! :)

DrexlSpivey

1:28 am on May 24, 2007 (gmt 0)

10+ Year Member



Hi,

thanks for that.

1) checking access logs, I have HTTP code 200, so looks ok.

The correctly working .ASP file also gives code 200. (Obviously the .ASP is logged as 2 requests, as the back button is preventing caching - the PHP code only logs 1 request as it is pulling from the cache the second time).

2) white space, etc: my code is exactly as shown above, i.e. stripped down to the bare minimum, so unless I'm missing something I can't see how anything would have been sent in advance of the header.

3) Firefox 2.0 and 2.0.0.3: neither the PHP or ASP worked correctly in either version of Firefox - both are allowing caching.

This is really puzzling me ... the script is so simple there should be nothing to go wrong.

Any further thoughts much appreciated.

HoboTraveler

1:56 pm on May 24, 2007 (gmt 0)

10+ Year Member



Lets call the previous page as page b.

I guess you get to page b through a submit action on page a correct?

You could have page b reload itself if the page is called through a method other than the submit action on page a. In this case it would be the back button.

DrexlSpivey

5:57 pm on May 24, 2007 (gmt 0)

10+ Year Member



If loading page b using a submit, there is generally no problem in practice; I use meta tags in the header and they seem to work in that situation:
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">

The problem is with the back button - a user clicking on this (even after logging out) will be able to see the cached pages.

(The example in my original post is a simplified version without the meta tags or anything else, but it still has the same problem of allowing caching).

HoboTraveler

10:19 am on May 25, 2007 (gmt 0)

10+ Year Member



Maybe this will work?

<HTML>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</HEAD>
<BODY>

Browser content

</BODY>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</HEAD>
</HTML>

vincevincevince

10:27 am on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using POST between every page is a sure way to avoid this problem. Pressing back will give the user a choice between resubmitting or cancelling and staying where they are.

GET is not for doing anything which changes something serverside - so it should never matter that the user can go backwards. GET is for 'getting' information only - not for changing it.

POST is for changing things - giving new information or instructions to edit the contents of the database or similar.

If you stick to those guidelines then you shouldn't have a problem with allowing use of the back button.

Receptional Andy

11:15 am on May 25, 2007 (gmt 0)



The following works for me:


Header( "Last-Modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
Header( "Expires: " . gmdate( "D, j M Y H:i:s", time() ) . " GMT" );
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" ); // HTTP/1.0

DrexlSpivey

3:49 pm on May 28, 2007 (gmt 0)

10+ Year Member



Thanks Folks!

I tried the POST idea, but it didn't seem to help.

Same for the other headers, and also for
Header( "Cache-control: private", FALSE );

Apparently my original code does prevent caching in IE7, but not in IE6.

Will include all these headers anyway, in the hope that at least they work in some browsers some of the time, but have to accept that I can't depend on them.

Anyway, thanks for the ideas.