search
Homephp教程php手册php 表单令牌防止重复提交原理

在生成表单的时候,为防止表单重复提交。在form表单中添加一个隐藏的input标签来存放令牌,等到提交的时候,和表单一起提交,提交以后和生成的session值作比较,通过这种方式来达到防止重复提交的目的,简要代码如下.

例子,session方法,代码如下:

<?php 
if($_POST){ 
    if($_SESSION["token"]!=$_POST["token"]){ 
        die("非法提交!"); 
    }else{ 
        echo "安全提交!"; 
    } 
} 
$_SESSION["token"]=md5(rand(1,999));//没生成一次表单,修改一次值,不要在post处理前修改它,不然无法比对
?>
<form action="1.php" method="post"> 
<input type="hidden" value="<?php echo $_SESSION["token"]?>" name="token"> 
<input type="submit" value="提交" /> 
</form>

例子,cookie+md5方法,代码如下:

<?php 
$value = $_COOKIE["value"]; //读取上一次设置的Cookie值 
if(count($_POST)) { 
$long = ""; 
while(list($key,$value)=each($_POST))$long.=$value; 
$hash = md5($long); 
setcookie("value",$hash,time()+60*60); //重新设置cookie 
} 
if($value!=$hash) { 
// 如果两次的MD5值不一样就对数据进行进一步操作 
} else { 
//如果两次MD5的值相同,告知用户提交失败 ,为重复提交表单 
}
?>


本文地址:

转载随意,但请附上文章地址:-)

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools