Home > Backend Development > PHP Tutorial > PHP uses cookies to prevent repeated submission solutions_PHP tutorial

PHP uses cookies to prevent repeated submission solutions_PHP tutorial

WBOY
Release: 2016-07-13 16:55:48
Original
1030 people have browsed it

This article introduces the solution on how to use cookies to prevent users from repeatedly submitting the same data. The

principle is
If the data passes the two verifications above, it means that the data is legal and valid. At this time, we concatenate the submitted data into a string and encrypt it with MD5 to get an MD5 value. Then we put this value into the client through Cookie. When the user submits the form next time, we will repeat this step. , and read the MD5 value in the cookie and compare it. If they are the same, it can be concluded that the form submitted twice by the user is the same, otherwise replace the cookie!

The code is as follows
 代码如下 复制代码
//……
$lasthash = $HTTP_COOKIE_VARS["lasthash"]; //读取上一次设置的Cookie值

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); //重新设置cookie
}

if($lasthash!=$hash) {
// 如果两次的MD5值不一样就对数据进行进一步操作
}
else {
//如果两次MD5的值相同,告知用户提交失败
}
//……
?>
Copy code
//……

$lasthash = $HTTP_COOKIE_VARS["lasthash"]; //Read the last cookie value set

if(count($HTTP_POST_VARS)) { While(list($key,$value)=each($HTTP_POST_VARS))$long.=$value; $hash = md5($long); setcookie("lasthash",$hash,time()+60*60*24*30); //Reset cookie } if($lasthash!=$hash) { // If the two MD5 values ​​are different, perform further operations on the data } else { //If the two MD5 values ​​are the same, inform the user that the submission failed }
//……

?>
http://www.bkjia.com/PHPjc/631655.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631655.htmlTechArticleThis article introduces a solution on how to use cookies to prevent users from repeatedly submitting the same data. The principle is that if the data Passed the above two verifications, indicating that the data is legal and valid...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template