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);
}
Step 2. Set hidden field in your form :
<input type="text" name="TOKEN" value="{:session('TOKEN')}">
Step 3. Judge the token if exists:
<blockquote>来源:https://my.oschina.net/caomenglong/blog/728908</blockquote>