我如何使令牌过期时间 [英] How do i make a token expire time
本文介绍了我如何使令牌过期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前有生成令牌的功能,但我如何让它过期?此外,令牌的良好保质期是多少?
I currently have functions to generate a token, but how would i go about making it expire?Also, what would be a good shelf-life for the token?
代币生成代码:
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;
}
推荐答案
最佳实践是有一个数据库表来存储创建的令牌信息...
Best practice is to have a database table that stores the information of tokens created...
id | expiry_timestamp | token ...
然后编辑代码以存储使用其到期时间戳创建的每个令牌...
Then edit the code to store each token created with its 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并确认它是否大于当前时间戳
Just incase you want to check if it has expired, you can use another function to fetch its expiry_timestamp and confirm whether or not, it is greater than the current timestamp
这篇关于我如何使令牌过期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文