忘记密码页面,创建生成的密码以通过电子邮件发送给用户.

All*_*lie 0 php sql phpmyadmin

我正在尝试创建一个忘记密码页面.我听说通过电子邮件将原始密码发送给用户并不是一个好主意所以我试图创建一个随机确认密码,他们可以用来登录他们的帐户,然后将密码更改为他们想要的任何密码.到目前为止,问题是它说用户的电子邮件实际上并不在数据库中.另外,我应该更新数据库以存储随机密码还是我的工作方式?我的数据库有表用户名,fristname,电子邮件和密码.我要求用户以html格式发送电子邮件地址,然后将其发送到此php表单.这是我第一次尝试这样做,因此它可能会有很多错误,但我找到了一个教程来帮助一些,所以它不应该.谢谢您的帮助.

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Sending Password</title>
    </head>
    <body>
        <?php
      $db_server = "server";
       $db_username = "name";
       $db_password = "pass";

       $con = mysql_connect($db_server, $db_username, $db_password);if (!$con)
                {
                    die('Could not connect: ' . mysql_error());
                }

               $database = "Test_Members";  

              $er = mysql_select_db($db_username);
        if (!$er) 
        {
         print ("Error - Could not select the database");
         exit;
        }        

        //include "session.php";


function createRandomPassword() {



    $chars = "abcdefghijkmnopqrstuvwxyz023456789";

    srand((double)microtime()*1000000);

    $i = 0;

    $pass = '' ;



    while ($i <= 7) {

        $num = rand() % 33;

        $tmp = substr($chars, $num, 1);

        $pass = $pass . $tmp;

        $i++;

    }
    return $pass;

}

$password = createRandomPassword();
$password =$_P0ST['password']; 
      $email = $_P0ST['email']; 

        $tbl_name=Account_Holders;


        $sql="SELECT password FROM $tbl_name WHERE email='$email'";
        $result=mysql_query($sql);

        // if found this e-mail address, row must be 1 row
        // keep value in variable name "$count"
        $count=mysql_num_rows($result);


        // compare if $count =1 row
        if($count==1){

        $rows=mysql_fetch_array($result);

        // keep password in $your_password
        $your_password=$rows['password']; //will this replace the users password with the random one? That is what I am attempting to do here. 

        // send e-mail to ...
        $to=$email;

        // Your subject
        $subject="Your Password";

        // From
        $header="from: Feed The Students";

        // Your message
        $messages= "Your password for login to our website \r\n";
        $messages.="Your password is $your_password \r\n";
        $messages.="Please change this password for security reasons. Thank you. \r\n";

        // send email
        $sentmail = mail($to,$subject,$messages,$header);

        }

        // else if $count not equal 1
        else {
        echo "Sorry we did not find your email in our database.";
        }

        // if your email succesfully sent
        if($sentmail){
        echo "Your password has been sent to your email address.";
        }
        else {
        echo "We can not send your password at this time.";
        }



        ?>


    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

nag*_*eeb 5

首先,您不应该将用户的密码存储在数据库中 - 而不是以明文形式存储.我将在解决密码重置问题之前解决这个问题,因为它们已连接.

一种常见的技术是使用带盐的单向散列算法转换密码(有关更多信息,请参阅维基百科的Salt定义),然后当用户尝试登录时,执行相同的加密并比较散列.

为了使事情变得更复杂,尝试在每个哈希上更改salt.

这是我使用的一步一步的方法:

  1. 用户创建密码(通过注册表)
  2. 函数创建随机盐然后加密密码,在这种情况下,使用SHA256算法并使用随机创建的盐.

    $password_salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));

    $password_hash = hash('sha256', $salt . $password);

  3. 将$ password_hash和$ password_salt保存到users表.这些将在以后用于在用户尝试将来登录时进行身份验证.

  4. 当用户登录时,检查用户名/电子邮件/登录名,如果找到,则从users表中获取哈希值和salt,并将db中的密码哈希值与执行相同函数返回的哈希值进行比较,这些哈希值作为密码输入.

    $salt = $user_record['password_salt'];

    $entered_hash = hash('sha256', $salt . $entered_password);

在上面的例子中,如果$ entered_pa​​ssword_hash == $ user_record ['password_hash']($ user_record,来自您的数据库),则验证密码.

注意为了使事情变得更复杂,我实际上将密码哈希和salt存储在db中的相同字段中.您可以将它们以某种方式连接在一起,这样如果您的数据库受到损害,密码字段对于检索用户密码实际上是无用的.

现在,当您想要解决忘记的密码时,您可以创建一个随机密码,但仍然会通过电子邮件发送给用户,这不是一个好主意,即使对于临时密码也是如此.我建议通过电子邮件向用户发送一个单击链接,将其引导至密码重置页面.以下是该过程的几个步骤:

  1. 创建一个包含两列的email_authentication表 - userid和key.这将包含将用户的ID与随机生成的密钥相关联的信息.
  2. 创建一个页面(例如:reset.php),它需要一个URL参数(例如$ k),然后在email_authentication表中查找该参数.如果在表中找到匹配的记录,请删除该记录,然后重定向到允许同一记录中的用户标识的密码的页面.
  3. 用户忘记密码,单击忘记密码链接并输入正确的电子邮件地址.
  4. 记录在包含其用户ID的email_authentication表中创建,并为该密钥生成随机字符串(大约32个字符长)(例如:'r4nd0mstr1ng')
  5. 通过urlstring中生成的密钥,向用户发送指向您在步骤2中创建的页面的链接.(例如:http://foo.com/reset.php?k = r4nd0mstr1ng
  6. 用户单击电子邮件中的链接,reset.php中的脚本在email_authentication表中查找"r4nd0mstr1ng"并发现它与用户ID相关联,并将它们重定向到允许他们重置密码的表单.
  7. 密码重置后,请不要将其保存为明文!!

有关更多信息(可能是密码散列技术的更好解释,请查看HeyBigName的这篇文章.它基于CodeIgniter,但所包含的功能是普遍适用的.