Preventing form resubmission

前端 未结 12 935

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:54

    There are two parts to the answer:

    1. Ensure duplicate posts don't mess with your data on the server side. To do this, embed a unique identifier in the post so that you can reject subsequent requests server side. This pattern is called Idempotent Receiver in messaging terms.

    2. Ensure the user isn't bothered by the possibility of duplicate submits by both

      • redirecting to a GET after the POST (POST redirect GET pattern)
      • disabling the button using javascript

    Nothing you do under 2. will totally prevent duplicate submits. People can click very fast and hackers can post anyway. You always need 1. if you want to be absolutely sure there are no duplicates.

    0 讨论(0)
  • 2020-11-22 09:55

    The only way to be 100% sure the same form never gets submitted twice is to embed a unique identifier in each one you issue and track which ones have been submitted at the server. The pitfall there is that if the user backs up to the page where the form was and enters new data, the same form won't work.

    0 讨论(0)
  • 2020-11-22 10:00

    Try tris:

    function prevent_multi_submit($excl = "validator") {
        $string = "";
        foreach ($_POST as $key => $val) {
        // this test is to exclude a single variable, f.e. a captcha value
        if ($key != $excl) {
            $string .= $key . $val;
        }
        }
        if (isset($_SESSION['last'])) {
        if ($_SESSION['last'] === md5($string)) {
            return false;
        } else {
            $_SESSION['last'] = md5($string);
            return true;
        }
        } else {
        $_SESSION['last'] = md5($string);
        return true;
        }
    }
    

    How to use / example:

    if (isset($_POST)) {
        if ($_POST['field'] != "") { // place here the form validation and other controls
        if (prevent_multi_submit()) { // use the function before you call the database or etc
            mysql_query("INSERT INTO table..."); // or send a mail like...
            mail($mailto, $sub, $body); // etc
        } else {
            echo "The form is already processed";
        }
        } else {
        // your error about invalid fields
        }
    }
    

    Font: https://www.tutdepot.com/prevent-multiple-form-submission/

    0 讨论(0)
  • 2020-11-22 10:02

    You can use replaceState method of JQuery:

    <script>
       $(document).ready(function(){
       window.history.replaceState('','',window.location.href)
       });
    </script>
    

    This is the most elegant way to prevent data again after submission due to post back.

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 10:04

    Well I found nobody mentioned this trick.

    Without redirection, you can still prevent the form confirmation when refresh.

    By default, form code is like this:

    <form method="post" action="test.php">

    now, change it to <form method="post" action="test.php?nonsense=1">

    You will see the magic.

    I guess its because browsers won't trigger the confirmation alert popup if it gets a GET method (query string) in the url.

    0 讨论(0)
  • 2020-11-22 10:04

    The PRG pattern can only prevent the resubmission caused by page refreshing. This is not a 100% safe measure.

    Usually, I will take actions below to prevent resubmission:

    1. Client Side - Use javascript to prevent duplicate clicks on a button which will trigger form submission. You can just disable the button after the first click.

    2. Server Side - I will calculate a hash on the submitted parameters and save that hash in session or database, so when the duplicated submission was received we can detect the duplication then proper response to the client. However, you can manage to generate a hash at the client side.

    In most of the occasions, these measures can help to prevent resubmission.

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