Php uses cookies to prevent repeated forms from being submitted.

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. In actual web development, it is worth noting to prevent users from submitting form data repeatedly. First, repeated data submission may cause database storage errors, such as duplicate primary keys. In addition, if the user is unintentional, but I

Welcome to the Linux community forum and interact with 2 million technical staff> in actual web development, it is worth noting to prevent users from submitting form data repeatedly. First, repeated data submission may cause database storage errors, such as duplicate primary keys. In addition, if the user is unintentional, but I

Welcome to the Linux community forum and interact with 2 million technicians>

In actual web development, it is worth noting to Prevent Users From repeatedly submitting form data.

First, repeated data submission may cause database storage errors, such as duplicate primary keys. In addition, if the user does not intend to, but the PHP program causes repeated submissions, then the database will be ***** junk data! These junk data can cause problems with the entire PHP system query-this is very bad!

Therefore, in PHP Web programming, it is crucial to prevent users from submitting repeated data intentionally or unintentionally. There are multiple ways to prevent users from submitting forms repeatedly. For example, you can disable the "Submit" button after the user submits the form, or immediately jump to a page unrelated to the form after the user submits the form, these are good methods! Today I want to talk about another method: using the client cookie to verify whether the submitted Form Content is repeated. This method can be very useful in specific scenarios, of course, we not only need to learn this method, but also need to understand the role of PHP cookies in daily programming-most people are unfamiliar with cookies.

The specific method is as follows:

When the user clicks the "submit" button of the form and the content of the Form filled by the user passes the front-end js verification (we verify the first step of user input );

Next we will re-verify the integrity and legitimacy of form data on the PHP program on the backend server-this is very important. For more information about HP program security, see: Suggestions on PHP code security

If the data passes the preceding two verifications, the data is valid and valid. At this time, we concatenate the submitted data into a string and use MD5 encryption to obtain an MD5 value. Then we put this value on the client through cookies, when the user submits the form next time, we repeat this step and read the MD5 value in the Cookie for comparison, if they are the same, we can conclude that the two submitted forms are the same. Otherwise, this Cookie will be replaced!

The Code is as follows:

$ Value = $ _ COOKIE ["value"]; // read the Cookie value set last time

If (count ($ _ POST )){

$ Long = "";

While (list ($ key, $ value) = each ($ _ POST) $ long. = $ value;

$ Hash = md5 ($ long );

Setcookie ("value", $ hash, time () + 60*60); // reset the cookie

}

If ($ value! = $ Hash ){

// Perform further operations on the data if the MD5 values are different twice

} Else {

// If the two MD5 values are the same, the user is notified that the submission failed and the form is submitted repeatedly.

}

?>

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.