Php 密码重置链接到期

Php 密码重置链接到期,php,passwords,password-recovery,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"]) { //

我想知道是否有人能帮助我

我一直在对“密码重置”过程进行大量研究,从我找到的一个教程中,我能够将提供此功能的以下代码组合在一起

忘记密码

<?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.";
}

?>

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


这就是您的意思吗?

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


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

您的所有PHP文件中是否都有DB连接数据?您好,没有,我只是在编写脚本时使用它。然后将它们更改为处理一个文件。是的,这就是我想要实现的。问候