Php solves and prevents repeated submission of form forms.

Source: Internet
Author: User

Php solves and prevents repeated submission of form forms.

Preface

Why should we avoid repeated submission of form forms? Because we do not want our servers to repeat unnecessary data and avoid duplicate data in our databases, avoiding repeated forms is also a manifestation of making our website safer.

First, let's take a look at the situations that lead to repeated submission of forms. We know under which circumstances the repeated submission of forms can process repeated submission of forms from the root cause.

In the following case, the form will be submitted repeatedly:

Click Submit twice.

Click Refresh.

Use the browser back button to repeat the previous operation, resulting in repeated form submission.

Use the browser history to repeat the submission form.

Repeated HTTP requests in the browser.

Webpages are maliciously refreshed.

Below are several solutions:

I. Click the js setting button and it turns 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, it becomes gray and you cannot click it. If you need to submit the form again, you need to refresh the page, enter the data, and submit again.

Ii. Use session

InsessionPut a special sign in. When a form page is requested, a special character flag string is generated, which exists.sessionAnd put it in the hidden fields of the form. When accepting and processing form data, check whether the identification string exists, delete it from the session immediately, and then process the data normally.

If no valid flag string is found in the Form submission, it indicates that the form has been submitted and this submission is ignored.

This makes your web application more advanced.XSRFProtection

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

$code = mt_rand(0,1000000);

Stored in the hidden input box of the form:

< input type=”hidden” name=”code” value=””>

The PHP code on the receiving page is as follows:

<? Phpsession_start (); if (isset ($ _ POST ['code']) {if ($ _ POST ['code'] = $ _ SESSION ['code']) {// submitted the form repeatedly} else {$ _ SESSION ['code'] =$ _ POST ['code']; // store code }}?>

Iii. Use cookies

Principles andsessionButcookiesOnce the user's browser is disabledcookiesThis 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 a form ";}

4. Jump using the header Function

Once the user clicks the submit button, after the data is processed, the page jumps to another page.

If (isset ($ _ POST ['submit ']) {header ('location: success. php'); // after processing data, go to another page}

5. Use the database to add Constraints

Directly add a unique constraint to the database or create a unique index. Once you find that the user has submitted the statement repeatedly, a warning or prompt is directly thrown, or you can only process the data submitted for the first time. This is the most direct and effective method, it is required that the database design and architecture should be fully considered in the early stage.

6. Post/Redirect/Get mode.

Execute page redirection after submission. This is calledPost-Redirect-Get (PRG)Mode. In short, after a user submits a form, you can execute a client redirection and go to the successful submission information page.

If (isset ($ _ POST ['action']) & $ _ POST ['action'] = 'submitted') {// process data, such as after inserting data, go to other page headers ('location: submits_success.php ');}

Summary

The above are several methods for PHP to solve and avoid repeated submission of form forms. Through the above methods, repeated submission caused by pressing F5 can be avoided, in addition, it does not receive a warning that the browser forms are repeatedly submitted, and can also eliminate the same problem caused by pressing the browser forward and backward. I hope this article will help you in your 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.