Preventing form resubmission

前端 未结 12 937

Page one contains an HTML form. Page two - the code that handles the submitted data.

The form in page one gets submitted. The browser gets redirected to page two. P

12条回答
  •  渐次进展
    2020-11-22 09:52

    I really like @Angelin's answer. But if you're dealing with some legacy code where this is not practical, this technique might work for you.

    At the top of the file

    // Protect against resubmits
    if (empty($_POST))  {
       $_POST['last_pos_sub'] = time();
    } else {
         if (isset($_POST['last_pos_sub'])){
            if ($_POST['last_pos_sub'] == $_SESSION['curr_pos_sub']) {
               redirect back to the file so POST data is not preserved
            }
            $_SESSION['curr_pos_sub'] = $_POST['last_pos_sub'];
         }
    }
    

    Then at the end of the form, stick in last_pos_sub as follows:

    >
    

提交回复
热议问题