开发者

Forgot Login - PHP Issue

I have a login system which takes usernames and passwords.

Upon creating an account, the user enters their details (name, address, e-mail password etc). I md5 the passwords and store them in the database....

My question is how do I implement a forgot login page having md5'd the password....I wanted to create some auto-email servic开发者_运维技巧e that e-mailed the password to the e-mail address provided..but I am struggling to figure out how to implement this given that I never have access to the cleartext password, only the encrypted password...


You don't email users their passwords. You typically email them a new auto-generated temporary password.


You generate a new random password and e-mail them that. Recovering the password is not practical, which is part of the reason to store the hash instead of the password. It makes a security breach less significant.


You don't need to send user old password at all.

You probably can send 1-time authentication link (that logs user in without password) or send just new generated password.


You'll notice that most (reputable!) websites only let you reset the password, not give you your old password.

This is why. :)


When you use md5 passwords, it would be near impossible to get the plaintext password. What you need to do is "reset" the password: Set it to something else and email it to the user


Here are some options:

  • During sign up, you can ask the user to set up some "secret questions and answers".

    • When the user initialises the reset password function, he can answer those questions and reset his password.
    • Or, once the questions are answered correctly, you can email him a new temporary password.
  • You can email them a special link. Upon clicking this link, they can set a new password.


It's a bad practice (and pretty much impossible once the password is md5'ed) to send emails containing the user's password. One alternative is:

Take the user's email and send a link to the email that allows the user to change their password. The link should be stored in another DB table and should have some sort of long unique id associated with it. When the link is requested, show a form to change the password. Then, md5() the new password and replace the user's password in the database.

if($_POST['submit'] == 'Send Reset Password Link') {
    if(//User exists & email is valid (check mysql)) {
        $db->prepare('INSERT INTO reset VALUES('', :email, :linkid)');
        //etc...
        mail($email, 'Password Reset for myawesomesite.com', 'Here is your link: http://www.myawesomesite.com/reset.php?id='.$linkid.'&email='.urlencode($email));
        echo "Email sent";
    }
}

reset.php:

if(isset($_GET['id']) && isset($_GET['email'])) {
    $q = $db->prepare('SELECT id FROM reset WHERE email=:email AND linkid=:linkid');
    $q->bindParam(':email', $_GET['email']);
    $q->bindParam(':linkid', $_GET['id']);
    $q->execute();

    if($q->rowCount() == 1) {
        //Show password reset form
    } else {
        echo "Reset link invalid";
    }
}

Then you can reset the password just be using UPDATE users WHERE email='' SET password=''

EDIT
I advise against sending users temporary passwords unless your forgot password form has a layer of security beyond the user's email. If it doesn't, anyone who knows a user's email can reset their password. Also, emails are typically unencrypted and I consider it not safe to send passwords (temporary or not) over email.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜