2012-01-14 48 views
2

我想知道是否有人可以帮助我。密码重置链接有效日期

我一直在对“密码重置”过程进行相当多的研究,并且从我发现的教程之一中,我已经能够将以下代码放在一起提供此功能。

忘记密码

<?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社区和其他许多人的帖子,但我一直没能找到我一直在寻找的东西。

我只是想知道是否有人可以帮助我,请给我一点指导,告诉我如何完成此任务。

非常感谢。

+0

你真的有*你的PHP文件中的数据库连接数据? – ThiefMaster 2012-01-14 15:38:35

+0

嗨,不,我只是在我将脚本放在一起的时候使用它。然后他们被改变去处理一个文件。亲切的问候 – IRHM 2012-01-14 16:37:37

回答

1

当请求密码重置时,用时间戳添加字段到users表。 当您检查密钥是否匹配时,查看时间戳以查看它的年龄。

这是你的意思吗?

+0

嗨,是的,这是我想要实现的。亲切的问候 – IRHM 2012-01-14 16:38:41

0

我这样做的方式是同时存储您发送给用户的散列值和从用户表中生成时的时间戳。

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

相关问题