9

I have tried every combination and permutation of meta tags that are supposed to stop a page from being cached, but Firefox STILL caches the page! I just need the URL to reload when a user presses the back button. Works fine in IE8.

I have tried all of these...

<meta http-equiv="Cache-Control" content="no-store" />
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="-1"/>
<meta http-equiv="Expires" content="Sat, 1 Jan 2000 00:00:00 GMT" /> 

...and I have also tried the following JavaScript...

<input type="hidden" id="refreshed" value="no"/>
<script type="text/javascript">

    onload=function(){
        var e=document.getElementById("refreshed");
        if(e.value=="no"){
            e.value="yes";
        }

        else{
            e.value="no";
            location.reload();
        }

    }

</script> 

... all to no avail. What am I missing here? Pages are generated with PHP if that matters.

UPDATE 1:

I have tried every suggestion thus far but I still cannot get this to work. When I use Chris's PHP code I use it like this...

<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<!--the rest of my page-->

.. and as you can see it is at the EXTREME top of my webpage, before the DOCTYPE header.

I have also experimented with session_start() but even after reading the manual I am not sure I am using it right. I was putting it right at the very top of my page as well.

I am open to ANY SUGGESTIONS that make this work without breaking other page functionality. I know I have seen pages that reload EVERY TIME the back button is used, HOW ARE THEY DOING IT?!

SOLVED!

Turns out I had multiple issues working against me, but through due diligence I was able to eliminate those issues and emerge victorious.

After Chris updated his code to...

<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo time();
?><a href="http://google.com">aaaaaaaaaaaaa</a>

I found that his code did indeed work when I used it EXACTLY how he had it with NOTHING else, but when I put it into my pages it didn't work. All of my pages are either .php or .html and all are attached to a DWT (Dynamic Web Template), so I was updating all of them at once with Chris's code. What I didn't realize was that the DWT starts RIGHT AFTER the DOCTYPE header, so the code was never inserted into my pages. I could find no way to make the DWT include the DOCTYPE header so I went into all my pages and manually inserted the code above the DOCTYPE header.

Next I found that even though my server is set to parse .htm and .html as .php the .html pages were generating an error at the very where I had inserted Chris's code saying something to the effect of "cannot modify headers, headers have already been sent". I didn't really care what my extensions were so I just changed all my .html extensions to .php extensions.

A final minor annoyance was that even though the page was now not being cached (just like I wanted) Firefox was placing the user at their last location on the previous page when they used the back button (i.e. if a user was at the bottom of page a when they navigated to page b, then the user used the back button on page b they would be returned to the bottom of page a, not the top of page a as desired). Trimming down my original JavaScript fixed this...

    <script type="text/javascript">

        onload=function(){
            document.getElementById('content').scrollTop=0;
        }

    </script>

Even though this seems very involved for such a simple problem I am glad it is fixed. Thanks for your help everyone (especially Chris).

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179

7 Answers7

9

This works for me

make a new php file. I can use the back and forward buttons and the number on the page always updates

<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo time();
?><a href="http://google.com">aaaaaaaaaaaaa</a>
goat
  • 31,486
  • 7
  • 73
  • 96
  • @chris I tired a variation of this (suggested here: http://stackoverflow.com/questions/3645609/reload-the-page-on-hitting-back-button )... Your is a little different so I will try it too. – ubiquibacon Jan 07 '11 at 21:45
  • @chris is it possible I am putting this in the wrong place? I have tried it in the `` tag, right after the `` tag, right before the ` tag, and at the very top of the page, before the `DOC TYPE`, none of which have worked. – ubiquibacon Jan 07 '11 at 22:06
  • You need to put it before EVERYTHING including any whitespace (spaces, newlines, etc) @typo. – Hello71 Jan 07 '11 at 22:09
  • @chris still will not work. Can someone confirm that ANY of these solutions actually work in Firefox? – ubiquibacon Jan 07 '11 at 22:19
  • @chris Since I am having so much trouble with this would you mind providing a verbose explanation of how I can use the code you posted "properly"? – ubiquibacon Jan 08 '11 at 17:11
  • @chris Ok, now we are getting some where. When I use your code EXACTLY as you have it it works great. Also when I use it in with the most basic HTML headers and tags (`DOCTYPE`, ``, etc.) it works fine, but when I put it into my full page it does not work. I'll mess with it some more and post an update soon. – ubiquibacon Jan 08 '11 at 18:53
  • @chris Finally got it figured out. The solution was a bit more manual than I would have liked, but it worked none the less. – ubiquibacon Jan 08 '11 at 20:53
3

You may add the following PHP Code in your PHP Page.

CodeIgniter Framework version:
$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
$this->output->set_header('Pragma: no-cache');

PHP version:

header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0',false);
header('Pragma: no-cache');

And also you may add following Java Script Code in Login/Master Page at <head> Section:

<script language="javascript" type="text/javascript">
    window.history.forward();
</script>

Compatibility verified in

  • Internet Explorer
  • Google Chrome
  • Mozilla Firefox
  • Opera
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

Check out live http headers, you'll see when you press the back button, no request is made. Which is done on purpose, so you don't loose what you may have typed into forms.

Edit

Some post on experts exchange said this worked, but its from 2006.

<META HTTP-EQUIV="Expires" CONTENT="0">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-store">

Then a javascript refresh

profitphp
  • 8,104
  • 2
  • 28
  • 21
  • I know I have used Firefox on sites where I have pressed the back button and the page reloads. I need this to work to help idiot proof my app for users, so I don't want to require them to install an add-on. – ubiquibacon Jan 07 '11 at 21:40
0
<meta http-equiv="Cache-Control" content="no-cache" />

I think the correct value is "no-cache"

nedk
  • 673
  • 3
  • 8
0
session_cache_limiter('nocache');

That should automatically generate all the required headers. Note that some browsers, such as Opera, still ignore these when using the back button.

EDIT

The relevant headers are as follows, in case you want to generate them some other way:

Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
kijin
  • 8,702
  • 2
  • 26
  • 32
  • 1
    You can send http header using the header() function, instead of using a session related function which only works if you use sessions. – goat Jan 07 '11 at 21:49
  • this is pretty similar to what chris posted, and I cannot get either of your suggestions to work. – ubiquibacon Jan 07 '11 at 22:48
0

Why? I mean, why do you want to do this? When I press the back button, I want the standard function that the browser provides - to go back to the previous page. I don't want a suprise.

If it is to stop an accidental resubmission of a POST, that can be accomplished easily by ending the POST handling with a redirect to display the desired reply using a GET. A later back into this page, will redisplay the redirected GET (or reload it from the cache which must have the same result). This is what the user expects and is harmless.

This has the added advantage that if your save has failed when updating the database, the display will display what is on the database, not what has been left in the variables.

Ian
  • 1,941
  • 2
  • 20
  • 35
  • 2
    It is because once the form has been submitted, IF the user presses the back button, the information they are viewing will be inaccurate. – ubiquibacon Jan 07 '11 at 22:12
0

You posted a comment:

It is because once the form has been submitted, IF the user presses the back button, the information they are viewing will be inaccurate.

If you want to prevent this you're doing it wrong! :)

What you need to do is a redirect when the form has been submitted.

<?php
    // code that handles the form submit
    header("Location: http://www.example.com/form-success.html"); // needs the absolute url (You can use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() for this)
    exit; // prevents further execution of the php code when while doing to redirect
?>
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 1
    I am already doing my redirect the way you suggest, the problem is that AFTER the redirect has happened IF a user presses the back button they are taken back to the previous page EXACTLY how it was, and it IS NOT being reloaded. I need it to be RELOADED. – ubiquibacon Jan 08 '11 at 17:48