发送安全临时密码

发布于 2024-12-11 08:04:39 字数 1611 浏览 0 评论 0原文

我正在创建一个忘记密码页面,并将通过电子邮件向用户发送临时密码,以便他们可以登录并重置密码。

创建密码时应该考虑什么,最好的方法是什么。

我的想法是这样的: $temporarypassword = sha1($_SERVER['REMOTE_ADDR'])

试图只允许他们从他们请求临时密码的 IP 地址登录。最好的方法是什么?

到目前为止的代码:

      if(strpos($_SERVER['HTTP_REFERER'],'domain.com') && ($_POST['forgotpasstoken'] == sha1($_SESSION['token'].'forgotpassword'))){
        if(isset($_POST['forgotemail']) && !empty($_POST['forgotemail'])){
            $email = mysql_escape_string(trim($_POST['forgotemail']));

            if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
                echo '<div class="error">Please enter a valid email address.</div>';
            } else {
                $sql = "SELECT email FROM users WHERE email = '$email' LIMIT 1";
                            $res = mysql_query($sql) or die(mysql_error());
                                if (mysql_num_rows($res) > 0) {

                                    //If email/user exists
            $temporarypassword = sha1($_SERVER['REMOTE_ADDR'])  
                //EMAIL PASSWORD HERE           

        echo '<div class="success">A temporary recovery password has been emailed to you.</div>';
                                    //If email/user exits
                                } else {
                                    echo '<div class="error">This email is not registered.</div>';
                        }
            }

        } else {
        echo '<div class="error">Please enter an email address.</div>'; 
        }

}

I am creating a forgotten password page and will be emailing a temporary password to the user so they can log in and reset their password.

What should I take into account when creating the password, what is the best method.

An idea I had is something like: $temporarypassword = sha1($_SERVER['REMOTE_ADDR'])

In an attempt to only allow them to login from the ip address where they requested the temp password. What is the best way to do this??

Code so far:

      if(strpos($_SERVER['HTTP_REFERER'],'domain.com') && ($_POST['forgotpasstoken'] == sha1($_SESSION['token'].'forgotpassword'))){
        if(isset($_POST['forgotemail']) && !empty($_POST['forgotemail'])){
            $email = mysql_escape_string(trim($_POST['forgotemail']));

            if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
                echo '<div class="error">Please enter a valid email address.</div>';
            } else {
                $sql = "SELECT email FROM users WHERE email = '$email' LIMIT 1";
                            $res = mysql_query($sql) or die(mysql_error());
                                if (mysql_num_rows($res) > 0) {

                                    //If email/user exists
            $temporarypassword = sha1($_SERVER['REMOTE_ADDR'])  
                //EMAIL PASSWORD HERE           

        echo '<div class="success">A temporary recovery password has been emailed to you.</div>';
                                    //If email/user exits
                                } else {
                                    echo '<div class="error">This email is not registered.</div>';
                        }
            }

        } else {
        echo '<div class="error">Please enter an email address.</div>'; 
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-12-18 08:04:39

仅使用随机字符串:用户很可能尝试从 iPhone 等登录,失败,请求新密码,并且仅当他在家里的电脑上时才打开链接。 IP不同,设备不同,一切都不同。

Use just a random string: it's more than likely that user tries to log in from e.g. iPhone, fails, requests a new password, and only opens the link when he's at his home PC. IPs are different, device is different, everything's different.

手心的海 2024-12-18 08:04:39

如果您通过电子邮件发送密码,则无法确保其完全安全。电子邮件以纯文本形式传输。正如 alf 所说,用户可以从与他们请求的 IP 地址不同的 IP 地址重置密码。

一种选择是创建一个随机字符串,然后在密码重置页面上显示其中一半(发出重置请求后),并在电子邮件中显示该字符串的一半。然后要求用户在表单中输入两部分内容,然后再让他们选择新密码。

If you're emailing the password, there is no way to make it fully secure. Email is transmitted in plain text. And like alf said, the user may reset the password from a different IP address than the one they requested it from.

One option would be to create a random string, then display half of it on the password reset page (after the reset request is made) and half of the string in the email. Then require the user to enter both halves in a form before letting them choose a new password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文