使用密码哈希为&#34生成令牌;记住我"

时间:2014-11-11 00:30:50

标签: php token remember-me

我试图通过散列他们的电子邮件地址为每个用户生成唯一令牌,当他们点击“记住我”按钮时,我想将令牌写入数据库。由于某种原因,这段代码导致错误,但我不确定错误在哪里。

    if (isset($_POST['remember']){
    $expDate1 = time() + (3600*24*30); // Sets the date to a month from now in milliseconds -> used for cookie
    $expDate2 = date('Y-m-d H:i:s', $expDate1); // Sets the date to a month from now in YYYY-MM-DD HH:ii:ss -> used for database
    $token = password_hash($Email, PASSWORD_DEFAULT);  // Generate a random token based on the user's email

    $sql = "INSERT INTO RememberMe (userID, token, expDate) VALUES ('$userID', '$token', '$expDate2')";
    $result = db_query($sql);

    if ($result === false){
        die("DIE!"); 
    }

    setcookie("monster", $token, $expDate1, "/");
}

0 个答案:

没有答案