Prevent Form resubmission upon hitting back button

丶灬走出姿态 提交于 2019-11-27 13:35:54

I know this question is old, but having this issue myself, two lines I've discovered that works are:

header("Cache-Control: no cache");
session_cache_limiter("private_no_expire");

There are two ways I know of to do this. The simple way and the hard way.

Regardless of the way, when you are dealing with a state-based page (using $_SESSION), which you should be doing to keep your pages "live" and under your control, is prevent the caching of all pages like this:

<?php
//Set no caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
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");
?>

The hard way involves generating an id and storing it somewhere on the page as a hidden input or a &_SESSION cookie. Then you store the same id on the server as a $_SESSION. If they don't match, a series of preprogrammed if else type statements cause nothing to happen with the page is resubmitted (which is what it tries to do when you click back).

The easy way is to simply redirect the user back to the form submission page if the form was submitted successfully, like so:

header('Location: http://www.mydomain.com/redirect.php');

I hope this helps!

tanios

One thing that might help is making your filter form use a GET method instead of POST.

Browsers usually prevent POST input from being automatically resubmitted, which is something they don't do when GET input is used. Also, this will let users link to your page using a filter.

An alternative solution that also works if/when the page is reloaded involves checking the post's originality using $_SESSION. In a nutshell, check for a unique or random string.

In the form, add an input element with a value set using rand() or microtime():

<input type="hidden" name="formToken" value="<?php echo microtime();?>"/>

And then wrap the PHP function to validate and parse the form data in an if block:

if(!isset($_SESSION['formToken']) || $_POST['formToken'] !== $_SESSION['formToken'])){
       $_SESSION['formToken'] = $_POST['formToken'];
      /*continue form processing */
}

I used the answer at How do I detect if a user has got to a page using the back button? to detect whether or not the visit was triggered by a browser's back button click, and then if that was the case, I used JavaScript to reload the page. When the page is reloaded, my code already handles the corresponding validations to make sure that the form is never submitted twice. The important part in my case was forcing the page reload when the form was revisited after clicking the browser's back button. This is my code in the URL where I wanted to apply this validation:

<script type="text/javascript">
    if (window.performance && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) {
        location.reload();
    }
</script>

header("Cache-Control: no cache");

session_cache_limiter("private_no_expire");

NOTE : After using the post data what you have submitted from form then,these two lines should be used at the end of function.so, when we are back to the redirected page ,it will not ask you to resubmit the page . This will work.

You need to remove the request which POST data from browser history

history.replaceState("", "", "/the/result/page")

See this answer

Also you may follow the Post/Redirect/Get pattern.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!