2

誰か助けてくれませんか。

「パスワードのリセット」プロセスについてかなりの調査を行ってきました。見つけたチュートリアルの 1 つから、この機能を提供する次のコードをまとめることができました。

パスワードをお忘れですか

<?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 コミュニティや他の多くの投稿を見てきましたが、探していたものを見つけることができませんでした。

誰かが私を助けて、これを達成する方法について少しガイダンスを与えてくれるかどうか疑問に思いました.

どうもありがとう。

4

2 に答える 2

1

パスワードのリセットが要求されたときのタイムスタンプを含むユーザー テーブルにフィールドを追加します。キーが一致するかどうかを確認するときは、タイムスタンプをチェックして、それがどれくらい古いかを確認します。

これはあなたが意味するものですか?

于 2012-01-14T15:30:19.510 に答える
0

これを行う方法は、ユーザーに送信するハッシュと、それが生成されたときのタイムスタンプの両方をusersテーブルに格納することです。

リセットページにアクセスしたら、再度生成するのではなく、データベース内のハッシュに対して提供するハッシュを確認します(このようにすると、最初に作成された方法を覚えておく必要がないため、真にランダムなハッシュを使用できます)タイムスタンプも確認してください。

于 2012-01-14T15:36:18.340 に答える