How do I make Firefox reload page when back button is pressed?

ubiquibacon picture ubiquibacon · Jan 7, 2011 · Viewed 14.3k times · Source

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

Answer

goat picture goat · Jan 7, 2011

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>