php防止重复提交表单
1.生成三个方法
//创建TOKEN
public function creatToken() {
$code = chr(mt_rand(0xB0, 0xF7)) . chr(mt_rand(0xA1, 0xFE)) . chr(mt_rand(0xB0, 0xF7)) . chr(mt_rand(0xA1, 0xFE)) . chr(mt_rand(0xB0, 0xF7)) . chr(mt_rand(0xA1, 0xFE));
Session::set('TOKEN',$this->authcode($code));
}
//判断TOKEN
function checkToken($token) {
if ($token == session::get('TOKEN')) {
Session::set('TOKEN', NULL);
return TRUE;
} else {
return FALSE;
}
}
/* 加密TOKEN */
public function authcode($str) {
$key = "ANDIAMON";
$str = substr(md5($str), 8, 10);
return md5($key . $str);
}
2.创建一个token
$this->creatToken();//创建一个token
3.前端设置一个input
<input type="hidden" id="TOKEN" value="{:Session('TOKEN')}">
4.防止重复提交如果重复提交跳转至相关页面
if (!$this->checkToken($data['token'])) {
return json(['code'=>400,'msg'=>'不可重复提交数据!!!']);
}