Generate token with password hash for "Remember Me"

976 views Asked by At

I was trying to generate unique token for each user by hashing their email address, when they click on the "Remember Me" button, and i want to write the tokens to the database. For some reason this block of code caused error, but I'm not sure where the error(s) is/are.

    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

There are 0 answers