How to avoid duplicate form submission

Client scheme

Disable the submit button.

After the form is submitted, use JavaScript to disable the submit button. This method prevents anxious users from clicking the button multiple times. But there is a problem. If the client prohibits JavaScript, this method will not work.

Use post / redirect / get mode

Perform page redirection after submission, which is called post redirect get (PRG) mode. In short, when the user submits the form, you perform a client redirection and go to the submission success information page. This can avoid the repeated submission caused by pressing F5, and the warning of repeated submission of browser forms will not appear. It can also eliminate the same problems caused by pressing forward and backward of browser.

Using cookie processing

Use cookies to record the submission status of the form. You can check whether the form has been submitted according to its status. If the client prohibits cookies, this method will not work. Please note this.

Server solution

Store a special flag in the session

Generate a unique identifier on the server side, store it in the session, write it into the hidden field of the form, and then send the form page to the browser. After entering the information, click Submit. On the server side, obtain the value of the hidden field in the form and compare it with the unique identifier in the session. It means that the request is processed for the first time, and then remove the unique identifier in the session; If the inequality description is submitted repeatedly, it will not be processed. This gives your web application more advanced xsrf protection.

Use the header function to turn to

When the user submits the form, the server immediately turns to other pages after processing. In this way, even if the user uses the refresh key, it will not lead to repeated submission of the form, because it has turned to a new page, and the page script has ignored any submitted data.

Add constraints to the database

Add unique constraints or create unique indexes in the database to prevent duplicate data. This is the most effective way to prevent duplicate data submission.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>