How php resolves and avoids duplicate submissions of form forms

Source: Internet
Author: User
In PHP when submitting a form, you may encounter a network speed and so on, causing the page to suddenly load slowly, the user repeatedly click the Submit button, will produce multiple data in the database, resulting in an uncontrolled situation. So how to avoid and solve this problem? Let's take a look below.

Objective

Why avoid duplicate submissions of form forms? Because we don't want our servers to repeat the unnecessary data, we're also avoiding duplication of data in our databases, and avoiding duplicate submissions is also a way to make our site more secure.

Take a look at what happens next, which causes the form to repeat itself, and to know which situations might occur when a duplicate submission of a form can be processed from the root cause of the form.

The following scenario 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: Using the JS Settings button click to turn Gray

<form name=form1 method= "POST" action= "/" Target=_blank><p><input type= "text" name= "t1″size=" 20″> <input type= "button" value= "Submit" onclick= "Javascript:{this.disabled=true;document.form1.submit ();}" ></p></form>

After clicking the button turned gray can not click, the user needs to submit the form again, you will refresh the page after re-fill the data and submit.

Second: Using session

sessionput a special symbol in the. When a form page is requested, a special character token string is generated, in the session form, and in the hidden field. When you accept processing form data, check that the identity string exists and immediately remove it from the session and process the data normally.

If there is no valid flag string found in the form submission, this indicates that the form has been submitted, ignoring the submission.

This gives your web app a higher level of XSRF protection

When the submitted page is loaded, a random number is generated,

$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 ')} {//duplicate submit form}else{$_ session[' code '] =$_post[' code '; Storage code}}?>

Third: Using cookies

The principle is session similar, but cookies once the user's browser is disabled cookies , this function becomes invalid.

if (Isset ($_post[' submit ')) {Setcookie ("Tempcookie", "", Time () +30), Header ("Location:". $_server[php_self]); exit () ;} if (Isset ($_cookie["Tempcookie"])) {Setcookie ("Tempcookie", "", 0); echo "You have submitted the form";}

Four: Using the header function to jump

Once the user clicks the Submit button, the data is processed and then jumps to another page

if (Isset ($_post[' submit ')) {header (' location:success.php ');//After processing data, turn to other pages}

Five: Using a database to add constraints

Directly in the database to add a unique constraint or create a unique index, once it is found that the user repeatedly submitted, directly throw a warning or prompt, or only to deal with the first data submitted, this is the most direct and effective method, requires a pre-database design and architecture to consider.

Six: Post/redirect/get mode.

Perform page redirection after commit, which is known as the Post-Redirect-Get (PRG) pattern. In short, when the user submits the form, you go to perform a client redirect and go to the Submit Success Information page.

if (Isset ($_post[' action ')) && $_post[' action '] = = ' submitted ') {//process data, such as inserting data, immediately turn to the other page header (' Location: Submits_success.php ');}

Summarize

The above is about PHP to solve and avoid the form forms of repeated submissions of several methods, through the above methods can avoid the user by the F5 caused by repeated submissions, and it will not appear repeated browser forms of the warning, but also to eliminate the browser forward and backward by the same problem caused by. I hope this article will bring some help to our study and work.

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.