原理在于生成一个随机字符串放在session里。提交表单后来验证这个字符串。可以做到防止他人自己写form来欺骗提交,重复提交或者双击提交。
token.php
',
'e',
'2',
'f',
'p',
'g',
')',
'?',
'h',
'i',
'x',
'u',
'j',
'k',
'r',
'l',
'3',
't',
'm',
'n',
'=',
'o',
'+',
'p',
'f',
'q',
'!',
'k',
'r',
's',
'c',
'm',
't',
'v',
'j',
'u',
'v',
'w',
',',
'x',
'i',
'$',
'y',
'z',
'*'
);
# array indice friendly number of chars;
$numchars = count($chars) - 1;
$token = '';
# create random token at the specified length
for ($i = 0; $i $token .= $chars[mt_rand(0, $numchars)];
# should token be run through md5?
if ($md5) {
# number of 32 char chunks
$chunks = ceil(strlen($token) / 32);
$md5token = '';
# run each chunk through md5
for ($i = 1; $i $md5token .= md5(substr($token, $i * 32 - 32, 32));
# trim the token
$token = substr($md5token, 0, $len);
}
return $token;
}
?>
form.php
action.php