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>
Copy after login
例子,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的值相同,告知用户提交失败 ,为重复提交表单 } ?>
Copy after login
本文地址:
转载随意,但请附上文章地址:-)
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation
1 months ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1392
52

