Home > Backend Development > PHP Tutorial > PHP simply uses token to prevent repeated submission of forms

PHP simply uses token to prevent repeated submission of forms

巴扎黑
Release: 2016-11-22 11:08:35
Original
1054 people have browsed it

<?php
/*
* PHP简单利用token防止表单重复提交
* 此处理方法纯粹是为了给初学者参考
*/
session_start();
function set_token() {
$_SESSION[&#39;token&#39;] = md5(microtime(true));
}
function valid_token() {
$return = $_REQUEST[&#39;token&#39;] === $_SESSION[&#39;token&#39;] ? true : false;
set_token();
return $return;
}
//如果token为空则生成一个token
if(!isset($_SESSION[&#39;token&#39;]) || $_SESSION[&#39;token&#39;]==&#39;&#39;) {
set_token();
}
if(isset($_POST[&#39;test&#39;])){
if(!valid_token()){
echo "token error";
}else{
echo &#39;成功提交,Value:&#39;.$_POST[&#39;test&#39;];
}
}
?>
<form method="post" action="">
<input type="hidden" name="token" value="<?php echo $_SESSION[&#39;token&#39;]?>">
<input type="text" name="test" value="Default">
<input type="submit" value="提交" />
</form>
Copy after login

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