Common forms repeatedly submit problem collation and solution

  • 2020-03-29 23:44:55
  • OfStack



Common repetitive submission questions
A> Click the submit button twice.
B> Click the refresh button.
C> Repeat the previous action using the browser's back button, causing the form to be submitted repeatedly.
D> Repeat the submission form using the browser history.
E> Repeated HTTP requests from the browser.

Second, the method to prevent the form from being submitted repeatedly
A> Disable the submit button. The button disabled now after the form is submitted either cancels the button's click event or the default event. This method prevents impatient users from clicking the button many times. The problem is that if you disable Javascript on the client side, this method will not work, and of course on modern web sites, there should be very few.

B> Post/Redirect/Get model. A page Redirect is performed after the commit, which is known as the post-redirect -Get (PRG) pattern. In short, when the user submits the form, you perform a client redirect to the submission success page. This avoids duplicate submissions caused by the user pressing F5, avoids the warning of duplicate browser form submissions, and eliminates the same problem caused by pressing browser forward and backward.

C> Use cookies to handle repeated form submissions
Implementation in PHP:
 
  lt;?php 
   if(isset($_POST['go'])){ 
   setcookie("tempcookie","",time()+30); 
   header("Location:".$_SERVER[PHP_SELF]);exit(); 
   } if(isset($_COOKIE["tempcookie"])){ 
   setcookie("tempcookie","",0);echo " You have already submitted the form "; 
   } 
   ?> 

D> A special flag is stored in the session. When a form page is requested, a special string of character tokens is generated, stored in the session and placed in the form's hidden field. When you accept the processed form data, check for the existence of the identity string, remove it immediately from the session, and process the data normally. If there is no valid flag string in the form submission, it means that the form has already been submitted. Ignore this submission. This gives your web application more advanced XSRF protection.

E> Add constraints to the database. Add a unique constraint to the database or create a unique index to prevent duplicate data. This is the most effective way to prevent duplicate data submissions.

Related articles: