Forcing a page to reload when the Back button is clicked

When my users login they are taken to welcomeBack.php. On this page it checks their values in the database to see if there are any campaigns that need to spawn through the use of a lightbox. If so, a light box is displayed with some options. One of the radio button options is to “View features and prices”. When they select that, and click the Continue button, they are redirected to the right page.

However, if they click their browser’s Back button at this point, it takes them back to welcomeBack.php and once again spawns the lightbox. I searched all over the web for the best way to handle this. I tried all sorts of things that others suggested using PHP and modifying the header. Some of it worked in one browser and other suggestions worked in other browsers. There was not a clear way to do this, across the board with all browsers.

Then I found someone mention using javascript. So the following is what works—sort of.

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

Now if the user clicks the Back button, the lightbox will appear for a slight second, and then the page reloads and everything is fine. Is there anything else I can do so that the lightbox doesn’t even appear for a slight second?

Thanks!

is the only real way of doing it - by setting the right combination of headers you should be able to get the page to reload almost all the time.

The JavaScript solution will only work in some browsers and only if JavaScript is enabled plus as you saw it only sort of works since at least part of the original page has to load from cache before it can determine whether to try to force the page to reload.

You could possibly keep the JavaScript in the hope that it will fix things for the rare situations where setting the headers doesn’t work but they should be so rare that it probably isn’t worth it.

Maybe I’m missing something, but couldn’t you set a cookie (which is valid for the session) once the lightbox has been displayed. You check for the cookie before displaying the lightbox and only show the lightbox if the cookie isn’t present.

Remember, it is pulling the page from the cache, it’s not reloading it. So any logic that was used to determine if a lightbox is needed will NOT be executed again when they click back.

I tried all types of headers, each which came recommended by various PHP developers. They all give mixed results on different browsers. Right now I have them all commented out, and am relying on the javascript. Should I just uncomment the code? Or is there one listed here that you feel should work in most major browsers?

//header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
//header('Pragma: no-cache'); // HTTP 1.0.
//header('Expires: 0'); // Proxies.
    
    
//header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
//header('Cache-Control: no-store, no-cache, must-revalidate');
//header('Cache-Control: post-check=0, pre-check=0', FALSE);
//header('Pragma: no-cache');
    
    
// This one seems to work in Safari but not Chrome
    
// header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
//header("Last-Modified: Tue, 15 Nov 2007 12:45:26 GMT");
//header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
//header("Cache-Control: private, no-store, max-age=0, no-cache, must-revalidate, post-check=0, pre-check=0");
//header("Pragma: no-cache");

Yeah, but isn’t that the point? You only want the lightbox to be shown once.

I’m not sure I have understood your problem correctly, but if you are looking for a way to display something in a lightbox once per browser session (the first time a user visits the page), then a cookie is the way to go.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.