Php 此持久登录身份验证的说明和实现

Php 此持久登录身份验证的说明和实现,php,cookies,Php,Cookies,我在php登录系统中遵循了这个答案(记住我) 我能够使用 $selector = base64_encode(random_bytes(9)); $authenticator = random_bytes(33); $token = hash('sha256', $authenticator); $expires = date('Y-m-d\TH:i:s', time() + 864000); $stmt2 = $pdo->prepare("INSERT INTO auth_token

我在php登录系统中遵循了这个答案(记住我)

我能够使用

$selector = base64_encode(random_bytes(9));
$authenticator = random_bytes(33);
$token = hash('sha256', $authenticator);
$expires = date('Y-m-d\TH:i:s', time() + 864000);

$stmt2 = $pdo->prepare("INSERT INTO auth_tokens (selector,token,userid,expires) VALUES (:selector, :token, :userid, :expires)");
$stmt2->bindParam(':selector', $selector);
$stmt2->bindParam(':token', $token);
$stmt2->bindParam(':userid', $userid);
$stmt2->bindParam(':expires', $expires);
$stmt2->execute();

setcookie(
        'remember',
         $selector.':'.base64_encode($authenticator),
         time()+86400,
         '/',
         false
);
我似乎不明白这部分页面加载时重新验证

if (empty($_SESSION['userid']) && !empty($_COOKIE['remember'])) {
list($selector, $authenticator) = explode(':', $_COOKIE['remember']);

$row = $database->selectRow(
    "SELECT * FROM auth_tokens WHERE selector = ?",
    [
        $selector
    ]
);

if (hash_equals($row['token'], hash('sha256', base64_decode($authenticator)))) {
    $_SESSION['userid'] = $row['userid'];
    // Then regenerate login token as above
}
}
1.$selector$authenticator变量中应该是什么

因为从代码中,有一个查询说从auth_令牌中选择,其中selector=$selector

2.每次重新加载页面时,选择器都会更改,导致其随机性。因此,如果$selector=base64_编码(随机_字节(9))当我运行此查询时,它与选择器列中的任何内容都不匹配*“SELECT*FROM auth_tokens,其中selector=$selector”*

有人解释了页面加载时重新验证的和一些示例代码

$selector
$authenticator
变量中应该包含什么

在以前的访问中设置的cookie内容:

setcookie(…, $selector.':'.base64_encode($authenticator), …);
每次重新加载页面时,选择器都会更改,导致其随机性

它不应该在页面重新加载时更改。应在成功登录后立即设置一次,并存储在两个位置(服务器数据库和浏览器cookie):


它从cookie中获取。所以不需要声明变量??
setcookie(…, $selector.':'.base64_encode($authenticator), …);
if ($login->success && $login->rememberMe) { // However you implement it
    $selector = base64_encode(random_bytes(9));
    $authenticator = random_bytes(33);
    …
}