Page 1 of 1

How to disable Back button

Posted: Wed Aug 11, 2010 2:57 am
by simonmlewis
Hi

I have a page that runs a query, then redirects (meta redirect) to another page, but I don't want the user to be able to 'go back' by clicking the BACK button on the browser.

A crude was to stop it is to redirect them to three pages (as backing backing backing.... with those pages redirecting forward), but it's a pain.

Can you just switch off the back button on the page itself? I was going to use this:
<a href="next.html" onclick="location.replace(this.href);return false;">TEST</a>
But the redirect cannot use this code - can it ?? If I can use this in a redirect:
echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=vouchercomplete&email=$email&vc=$code'>";
then I will - but how please?

Re: How to disable Back button

Posted: Wed Aug 11, 2010 8:24 am
by PHPHorizons
Hello simonmlewis,

You cannot disable the back button. Even if javascript could do that, the user can simply turn off javascript, or may have it turned off already.

The way to do this is to use window.location.replace('new_web_page.html'). That method will *replace* the current page in the history. Therefore, if they hit the back button, they go to the page *before* the form submit handler.

This also has the deficiency that if javascript is turned off, it will not work. But it benefits from not taking over the user's browser and disabling their back button.

Cheers :)

Re: How to disable Back button

Posted: Wed Aug 11, 2010 9:37 am
by simonmlewis
Sorry I don't understand how that works.
How do you input that into the code after the script is run?

Does it prevent them hitting 'backspace' and running a script on the previous page again?

Re: How to disable Back button

Posted: Wed Aug 11, 2010 10:02 am
by PHPHorizons
To understand this, you need to understand the concept of a browser history (i.e., when you hit the back button, you go to the previous page in the history). By replacing the form submit handler page in the history, one cannot go back to that page. You go to the page before that page.

The javascript code to do this is exactly as I outlined it: window.location.replace('new_web_page.html')

You need to replace new_web_page.html with the page you want the user to land on.

Cheers

Re: How to disable Back button

Posted: Wed Aug 11, 2010 10:06 am
by simonmlewis
ie
<script>
window.location.replace('apage.html')
</script>
to be shown on the resulting page?

Re: How to disable Back button

Posted: Wed Aug 11, 2010 10:47 am
by PHPHorizons
Yeah ;)

Let me know how that works out for ya.

Re: How to disable Back button

Posted: Wed Aug 11, 2010 11:10 am
by simonmlewis
Wow that's a little bit of genius!!!
Works brill.

Means someone can claim a voucher, but when I hit BACK, it just goes back to the Claim form.

Lovely. Thx.

Re: How to disable Back button

Posted: Wed Aug 11, 2010 11:55 am
by PHPHorizons
Hehe, wish I could claim that I invented it! But I am glad to be able to pass it on.

Cheers ;)

Re: How to disable Back button

Posted: Mon Feb 11, 2013 9:16 am
by simonmlewis
This issue I had before is now extended because of something else: what if I have POST data to that page, and then on Click next, and on the following page, I click BACK - I then have the issue of "Document Expired".
How do I capture in PHP if the page has been loaded, but there data expected is not there?
The "Next" button on the first POST page, is triggered if $calculated != NULL.

If it IS NULL, then it doesn't show 'Next'. But if it is, it shows the normal 'Calculate' box.

So how do I get around this? Without totally recoding it all to pass through things as a GET, meaning (I think) I have to change the URL.

Re: How to disable Back button

Posted: Mon Feb 11, 2013 9:12 pm
by Weirdan
The standard way to handle post is Post/Redirect/Get.