我目前有生成令牌的功能,但我将如何使其过期?此外,令牌的保质期是多少?
令牌生成代码:
function token($length = 40) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$token = 12000;
$token = srand(floor(time() / $token));
for ($i = 0; $i < $length; $i++) {
$token .= $characters[rand(0, $charactersLength - 1)];
}
return $token;
}
答案 0 :(得分:1)
最佳做法是拥有一个数据库表,用于存储创建的令牌信息......
id | expiry_timestamp | token ...
然后编辑代码以存储使用expiry_timestamp创建的每个标记...
function token($length = 40, $expiry) {
// Set expiry_timestamp..
$expiry_timestamp = time() + $expiry;
// Generate the token...
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$token = 12000;
$token = srand(floor(time() / $token));
for ($i = 0; $i < $length; $i++) {
$token .= $characters[rand(0, $charactersLength - 1)];
}
/** Do a quick manipulation in the token table...
* ...Connect to database table then execute following SQL statement..
* mysqli_query($link, "INSERT INTO token_table (token, expiry_timestamp) VALUES($token, $expiry_timestamp)");
*/
return array($token,$expiry);
}
只是想要检查它是否已过期,您可以使用其他功能获取其expiry_timestamp并确认是否大于当前时间戳