9
votes

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).

7
Shouldn't it read <input type="hidden" id="refreshed" value="yes"/> ?Adam
@Adam I don't think so, but I tried it both ways to make sure... no luck.ubiquibacon

7 Answers

9
votes

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>
3
votes

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
0
votes

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

0
votes
<meta http-equiv="Cache-Control" content="no-cache" />

I think the correct value is "no-cache"

0
votes
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
0
votes

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.

0
votes

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
?>