密码重置链接到期

时间:2012-01-14 15:13:09

标签: php passwords password-recovery

我想知道是否有人可以帮助我。

我一直在对“密码重置”过程进行大量研究,从我发现的其中一个教程中,我已经能够将以下代码放在一起,提供此功能。

忘记密码

<?php

// Connect to MySQL
$c = mysql_connect("host", "user", "password");
mysql_select_db("database", $c);

// Was the form submitted?
if ($_POST["ForgotPasswordForm"])
{
    // Harvest submitted e-mail address
    $emailaddress = mysql_real_escape_string($_POST["emailaddress"]);

    // Check to see if a user exists with this e-mail
    $userExists = mysql_fetch_assoc(mysql_query("SELECT `emailaddress` FROM `userdetails` WHERE `emailaddress` = '$emailaddress'"));
    if ($userExists["emailaddress"])
    {
                // Create a unique salt. This will never leave PHP unencrypted.
                $salt = "KEY";

        // Create the unique user password reset key
$password = md5($salt . $userExists["emailaddress"]);


        // Create a url which we will direct them to reset their password
        $pwrurl = "phpfile.php?q=" . $password;

        // Mail them their key
        $mailbody = "Dear user,\n\nIf this e-mail does not apply to you please ignore it. It appears that you have requested a password reset at our website \n\nTo reset your password, please click the link below. If you cannot click it, please paste it into your web browser's address bar.\n\n" . $pwrurl . "\n\nThanks,\nThe Administration";
        mail($userExists["emailaddress"], "", $mailbody);
        echo "Your password recovery key has been sent to your e-mail address.";
    }
    else
        echo "No user with that e-mail address exists.";
}

?>

重设密码

<?php

// Connect to MySQL
$c = mysql_connect("host", "user", "password");
mysql_select_db("database", $c);

// Was the form submitted?
if ($_POST["ResetPasswordForm"])
{
    // Gather the post data
    $emailaddress = mysql_real_escape_string($_POST["emailaddress"]);
    $password = md5(mysql_real_escape_string($_POST["password"]));
    $confirmpassword = md5(mysql_real_escape_string($_POST["confirmpassword"]));

    $q = $_POST["q"];

    $passwordhint = $_POST["passwordhint"];

    // Use the same salt from the forgot_password.php file
    $salt = "KEY";

    // Generate the reset key
    $resetkey = md5($salt . $emailaddress);

    // Does the new reset key match the old one?
    if ($resetkey == $q)
    {
        if ($password == $confirmpassword)
        {
            // Update the user's password
            mysql_query("UPDATE `userdetails` SET `password` = '$password', `passwordhint` = '$passwordhint' WHERE `emailaddress` = '$emailaddress'");
            echo "Your password has been successfully reset.";
        }
        else
            echo "Your password's do not match.";
    }
    else
        echo "Your password reset key is invalid.";
}

?>

我现在想添加一个我发送给用户的链接的定时到期时间。我一直在关注Stackoverflow社区和其他许多人的帖子,但我找不到我一直在寻找的东西。

我只是想知道是否有人可以帮助我,请给我一些关于如何实现这一目标的指导。

非常感谢。

2 个答案:

答案 0 :(得分:1)

在请求密码重置时,使用时间戳向users表添加字段。 当您检查密钥是否匹配时,请检查时间戳以查看它的年龄。

这是你的意思吗?

答案 1 :(得分:0)

我这样做的方法是存储发送给用户的哈希值和用户表中生成时的时间戳。

当他们访问重置页面时,检查他们对数据库中的哈希值给出的哈希值,而不是再次生成它(这样做就可以使用真正的随机哈希值,因为你不必记住它是如何在第一名)并检查时间戳。