【发布时间】:2016-11-10 11:58:42
【问题描述】:
正在创建一个应用程序...到目前为止一切都很好。在我的注册系统中使用了准备好的语句和密码散列,并且还尝试在我的表单字段中验证用户输入。为了完成这个系统,我需要创建一个忘记密码系统,这意味着用户可以请求新密码。
我有一个包含所有文件的测试站点,这意味着我可以在将其添加到生产站点之前测试它是否有效。
忘记密码后使用 mysqli,一旦一切正常,我将更新为准备好的,因为我仍在学习准备好的语句,这样做有助于我理解,所以不要判断。
我忘记密码的问题是密码一旦更改就不会更新。看到这个截图:http://prntscr.com/d5hage
如上所述,在我的注册中使用http://prntscr.com/d5hbg1 并在我的登录中验证。但是如何在我忘记密码中使用散列或如何更新它。在我下面的代码中使用了我知道的 md5 已损坏。请把我所有的编码都写在下面。
Reset_Password.php
<?php
// include connection
require_once('include/connection.php');
if(isset($_POST['submit'])){
$user_id = base64_decode($_GET['encrypt']);
$passnew = password_hash($password, $_POST['new_password'], PASSWORD_BCRYPT, array( 'cost' => 12 ) );
$sql = "UPDATE `olami560_test`.`user` SET `password` =? WHERE `user`.`id` =?";
$stmt = $con->prepare($sql);
$stmt->bind_param('si',$passnew, $user_id);
$stmt->execute();
if ($stmt->errno) {
echo "FAILURE!!! " . $stmt->error;
}
else echo "Password Changed Successfully.Click on link to login <a href='http://www.olaskee.co.uk/project/allocation/progress/index.php'>Login</a>{$stmt->affected_rows} rows";
$stmt->close();
}
?>
<form method="post" action="<?php echo $_SERVER['HTTP_REFERER']; ?>" >
<label>New Password</label>
<input type="password" name="new_password"/>
<input type="submit" name="submit" value="Reset" />
</form>
forgot_password.php
<?php
// include connection
require_once('include/connection.php');
if(isset($_GET) && !empty($_GET['email'])){
$email = mysqli_real_escape_string($con,$_GET['email']);
$query = "SELECT id
FROM `user`
WHERE `user_name` LIKE '".$email."'
OR `email` LIKE '".$email."'";
$result = mysqli_query($con,$query);
$Results = mysqli_fetch_array($result);
if(count($Results)>=1)
{
$query2 = "SELECT email
FROM `user`
WHERE `user_name` LIKE '".$email."'
OR `email` LIKE '".$email."'";
$result2 = mysqli_query($con,$query2);
$emailvalue = mysqli_fetch_array($result2);
//$token = md5(uniqid(rand(),true));
//$encrypt = md5($Results['id']);
$encrypt = base64_encode($Results['id']);
$message = "Your password reset link send to your e-mail address.";
$to = $emailvalue['email'];
$subject="Forget Password";
$from = 'leksmaster@gmail.com';
$body= 'Hi, <br/> User <br/>You Requested for Reset Password. <br><br>http://www.olaskee.co.uk/project/allocation/tms/reset_password.php?token='.$token.'&encrypt='.$encrypt.'&action=reset<br/> <br/>--<br>.olaskee<br>';
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$body,$headers);
echo $message;
}
else
{
$message = "Account not found please signup now!!";
echo $message;
}
}
?>
我希望已经提供了足够的解释让你理解。感谢任何意见。
【问题讨论】:
-
你的截图使用的是
password_hash,你为什么不使用同样的功能呢? -
为什么投票失败...我的请求或问题有什么问题。请分享您的意见。谢谢
-
当我使用该注意发生时,我仍然得到 0 行更新
-
在确保密码列为 60+ 的同时,不能将 MD5 与 password_hash() 混合使用,否则会静默失败。您需要阅读手册及其用法。
-
@Fred-ii-...感谢您的意见...您是对的,这是我这边的一个错误。现在已将其注释掉并更新密码,但使用 md5 算法而不是散列。检查我上面的更新代码。
标签: php mysqli prepared-statement password-hash