【发布时间】:2018-06-05 16:47:54
【问题描述】:
通过注释掉我发现这段代码的最后一行没有被注释掉
$query->bindParam(':password', $password);
是代码停止工作的那一行,我得到的只是一个
此页面无法正常工作 .... 目前无法处理此请求。 HTTP 错误 500"
但是,我不知道 bindParam() 的用法有什么问题。
我后来添加了这个:
$query = $conn->prepare('UPDATE users SET user_password = ? WHERE user_email = ?');
$query->execute(array($password, $email));
echo "Your password has been successfully reset.";
我再次更改了代码,现在它可以工作了,整个代码现在看起来像这样:
// Was the form submitted?
if (isset($_POST["ResetPasswordForm"])) {
// Gather the post data
include_once 'includes/database.inc.php';
$email = $_POST['email'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
$hash = $_POST["q"];
// Use the same salt from the forgot_password.php file
$salt = "498#2D83B631%3800EBD!801600D*7E3CC13";
// Generate the reset key
$resetkey = hash('sha512', $salt.$email);
// Does the new reset key match the old one?
if ($resetkey == $hash) {
if ($password == $confirmpassword) {
// has and secure the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Update the user's password
$sqlUpdate = "UPDATE users SET user_password = ? WHERE user_email = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sqlUpdate)) {
echo "SQL Failed";
exit();
} else {
mysqli_stmt_bind_param($stmt, "ss", $hashedPassword, $email);
mysqli_stmt_execute($stmt);
echo "Your password has been successfully reset2.";
exit();
} else {
echo "Your password reset key is invalid.";
exit();
}
}
?>
【问题讨论】:
-
“停止工作”是什么意思?您收到错误消息吗?如果你只是得到一个空白屏幕,你检查过你的 PHP 错误日志吗?或者你得到一个结果但不是你想要的?可能有一些细节对你来说似乎很明显,因为你已经盯着这个有一段时间了,但我们只有你告诉我们的。 (请edit问题更详细,不要试图将它们放入cmets。)
-
sha512 对于密码散列来说太快了。这是您需要使用的:php.net/password_hash
-
bindParam在发生错误时返回FALSE。检查返回值,以及 PHP 日志。 -
@Mike 问题一:你说的太快是什么意思?我可以回显散列密码。问题二(或注):见我的编辑。我试图抛出异常,但没有成功。