Php uses cookies to prevent repeated submission. Solution

Source: Internet
Author: User

The principle is
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: Copy code
<? Php
//......
$ Lasthash = $ HTTP_COOKIE_VARS ["lasthash"]; // read the last set Cookie value
 
If (count ($ HTTP_POST_VARS )){
$ Long = "";
While (list ($ key, $ value) = each ($ HTTP_POST_VARS) $ long. = $ value;
$ Hash = md5 ($ long );
Setcookie ("lasthash", $ hash, time () + 60*60*24*30); // reset the cookie
}
 
If ($ lasthash! = $ 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.
}
//......
?>

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.