How php resolves and avoids duplicate submissions of form forms

Source: Internet
Author: User
Tags setcookie

when you submit a form, you may encounter a speed, such as the page suddenly load slow, the user repeatedly click the Submit button, will produce multiple data in the database, resulting in an uncontrolled situation. For example, the following situation causes the form to be repeated: Click the Submit button two times. Click the Refresh button. Use the browser Back button to repeat the previous action, causing the form to be submitted repeatedly. Repeat the form by using browser history. The browser repeats the HTTP request. The webpage was maliciously refreshed. Here are a few workarounds: one: Use the JS Settings button to turn gray $ (document). Ready (function () {$ (input:submit). Click () {setTimeout (function () { Obj.disabled=true;}, -)  };}); Second: Use the session to load the submitted page, generate a random number, $code= Mt_rand (0,1000000), stored in the form's Hidden input box:< input type="Hidden"Name="Code"Value="">The PHP code on the receive page is as follows:<?Phpsession_start ();if(Isset ($_post['Code'])) {      if($_post['Code'] = = $_session['Code']){         //submit the form repeatedly}Else{$_session['Code'] =$_post['Code'];//Store Code    }  }?>Third: The use of cookies is similar to the session, but cookies once the user's browser disables cookies, this function is invalidif(Isset ($_post['Submit']) {Setcookie ("Tempcookie","", Time () + -); Header ("Location :". $_server[php_self]); exit (); } if(Isset ($_cookie["Tempcookie"]) {Setcookie ("Tempcookie","",0); Echo"you have already submitted a form"; } Four: Use the header function to jump once the user clicks the Submit button, finishes processing the data and jumps to another pageif(Isset ($_post['Submit']) {Header ('location:success.php');//after processing the data, turn to other pages} Five: Use the database to add constraints directly in the database to add a unique constraint or create a unique index, once the user repeatedly committed, throw a warning or prompt, or only deal with the first commit data, this is the most direct and effective method, requires the pre-database design and architecture to consider the comprehensive 

How php resolves and avoids duplicate submissions of form forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.