Prevent Form resubmission upon hitting back button

前端 未结 8 1993
无人及你
无人及你 2020-12-03 05:17

I have searched many posts here and elsewhere but can\'t seem to find a solution to my problem. I have a page which displays database entries: database.php. These entries c

相关标签:
8条回答
  • 2020-12-03 05:49

    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.

    0 讨论(0)
  • 2020-12-03 05:50

    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!

    0 讨论(0)
提交回复
热议问题