PHP Form token prevent repeat submit principle

Source: Internet
Author: User

Example, session method

  code is as follows copy code

<?p HP
if ($_post) {
    if ($_session["token"]!=$_post["token"]) {
         die ("illegal submission!") ");
   }else{
        echo "secure submission!";
   }
}
$_session["token"]=md5 (rand (1,999));//form not generated once, modify value once, do not modify it before post processing, or you cannot compare to Www.111cn.net
?>
<form action= "1.php" method= "POST"
<input type= "hidden" value= "<?php echo $_session[" token "]?>" Name= "token"
<input type= "Submit" value= "commit"/>
</form>

Example, Cookie+md5 method

The code is as follows Copy Code

<?php

$value = $_cookie["value"]; Read the cookie value that was last set

if (count ($_post)) {

$long = "";

while (list ($key, $value) =each ($_post)) $long. = $value;

$hash = MD5 ($long);

Setcookie ("Value", $hash, Time () +60*60);//Reset Cookies

}

if ($value!= $hash) {

Further operation of the data if the MD5 value is not the same two times

} else {

If the value of two times MD5 is the same, inform the user that the submission failed, and submit the form for duplicate

}

?>

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.