三、php防止表单重复提交的办法

In ThinkPHP5:

Step 1.

Add follow 3 functions in your BaseController:

//Create 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('TOKEN', $this->authcode($code));
}

//Judge TOKEN
public function checkToken($token) {
    if ($token == session('TOKEN')) {
        session('TOKEN', NULL);
        return TRUE;
    } else {
        return FALSE;
    }
}

/* Encrypt TOKEN */
public function authcode($str) {
    $key = "ANDIAMON";
    $str = substr(md5($str), 8, 10);
    return md5($key . $str);
}
三、php防止表单重复提交的办法_第1张图片
**创建token**
三、php防止表单重复提交的办法_第2张图片
**32个字符**

Step 2. Set hidden field in your form :


Step 3. Judge the token if exists:

三、php防止表单重复提交的办法_第3张图片

来源: https://my.oschina.net/caomenglong/blog/728908

你可能感兴趣的:(三、php防止表单重复提交的办法)