Php uses cookies to prevent repeated submission. Solution

Source: Internet
Author: User
This article introduces how to use cookies to prevent users from repeatedly submitting the same data. The principle is: if the data passes the two verification steps above, it indicates that the data is valid and valid.

This article describes how to use cookies to prevent users from repeatedly submitting the same data. The principle is: if the data passes the two verification steps above, it indicates that the data is valid and valid. at this time, we concatenate the submitted data into a string and encrypt it with MD5 to obtain an MD5 value.

Then, we put the value in the client through the Cookie. 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:

  1. //......
  2. $ Lasthash = $ HTTP_COOKIE_VARS ["lasthash"]; // read the last set Cookie value
  3.  
  4. If (count ($ HTTP_POST_VARS )){
  5. $ Long = "";
  6. While (list ($ key, $ value) = each ($ HTTP_POST_VARS) $ long. = $ value;
  7. $ Hash = md5 ($ long );
  8. Setcookie ("lasthash", $ hash, time () + 60*60*24*30); // reset the cookie
  9. }
  10.  
  11. If ($ lasthash! = $ Hash ){
  12. // Perform further operations on the data if the MD5 values are different twice
  13. }
  14. Else {
  15. // If the two MD5 values are the same, the user is notified that the submission failed.
  16. }
  17. //......
  18. ?>

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.