Prevent Form resubmission upon hitting back button

前端 未结 8 2038
无人及你
无人及你 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:45

    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():

    
    

    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 */
    }
    

提交回复
热议问题