我在数据库中没有更新密码哈希有一点问题,我正在努力看看我做错了什么。如果有人可以提供帮助,将不胜感激。
基本上我有一个 login.php 脚本,它过去只是将 md5 和 salt 用于用户的密码并将其存储在 mysql db 中,我知道它已经过时了。所以我最近尝试更新它以在用户下次登录时将用户密码更新为 PHP password_hash。
我首先通过 post 收集密码,然后调用 db 将其与 db 中的哈希值进行比较。如果密码验证我然后通过 password_needs_rehash 运行 db 密码以检查它是否需要重新散列。如果确实如此,那么它将生成一个新的 has 并将其存储到一个变量中,以便通过 mysqli 准备语句运行,该语句更新哈希以及他们上次登录的时间,否则它只会使用上次登录。
它似乎无法更新新的哈希(即使我已经检查过它是否已生成),但仍会更新查询中的时间。没有来自异常代码的电子邮件或任何要报告的 php 错误。
// Check if a newer hashing algorithm is available or if the cost has changed
if (password_needs_rehash($dbpass, PASSWORD_DEFAULT, $cost)) {
// If so, create a new hash, and replace the old one
$newHash = password_hash($password, PASSWORD_DEFAULT, $cost);
}
//update last login date
date_default_timezone_set("Europe/London");
$servertime = date("Y-m-d H:i:s");
// connect to db for mysqli
require('../dbconn/user.php');
// query to update password, increment number of logins and set last login date for user
$update_pass_stmt = $mysqli->stmt_init();
// if new hash has been generated
if (isset($newHash)) {
$q = "UPDATE users SET theirpassword=?, lastlogin=? WHERE email=? LIMIT 1";
if ($update_pass_stmt->prepare($q)) {
$update_pass_stmt->bind_param('sss', $newHash, $servertime, $email);
$update_pass_stmt->execute();
$update_pass_stmt->close();
}
} else {
$q = "UPDATE users SET lastlogin=? WHERE email=? LIMIT 1";
if ($update_pass_stmt->prepare($q)) {
$update_pass_stmt->bind_param('ss', $servertime, $email);
$update_pass_stmt->execute();
$update_pass_stmt->close();
}
}
if ($mysqli->error) {
try {
throw new Exception("MySQL error $mysqli->error <br> Query:<br> $q", $mysqli->errno);
} catch(Exception $e) {
$mess = "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br >";
$mess .= nl2br($e->getTraceAsString());
$contact_email = "webmaster@example.co.uk";
$message_sub = "Mysqli Login Query Error [EN-UUTIME01]";
$hdrs = "From: " . $contact_email . "\r\n";
$hdrs .= "Reply-To: ". $contact_email . "\r\n";
$hdrs .= "MIME-Version: 1.0\r\n";
$hdrs .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($contact_email, $message_sub, $mess, $hdrs);
}
$mysqli->close();
$_SESSION['internalerror'] = TRUE;
header("Location: ". $MM_redirectLoginFailed );
exit();
}
$mysqli->close();
我已经在代码的每个部分中重复了,它似乎正在运行到正确的 sql 语句。
我只能认为我要么错过了一个明显的错字,要么我的逻辑在某个地方有错误。
任何帮助总是很感激,谢谢。