【发布时间】:2019-07-31 23:11:47
【问题描述】:
我正在尝试使用 php 创建密码重置页面。但我遇到的问题是当我输入新密码然后按重置按钮时,程序将创建一个随机密码并将其保存在数据库中。不是我之前输入的那个。谢谢。
这是我使用的代码:
<?php
require_once 'inc/dbconnection.php';
if(isset($_POST["reset-password"])){
$name = $_GET["Fname"];
$password = trim($_POST["password"]);
$confirmPassword = trim($_POST["confirmPassword"]);
if($password == $confirmPassword) {
$password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare("UPDATE sec SET password= ? WHERE Fname = ?");
$stmt->execute(array($password,$name));
$affected_rows = $stmt->rowCount();
if($affected_rows) {
$success_message = "Password is reset successfully.<br>Now you are redirecting";
header("Refresh:3; url=loginSf.html");
} else {
$error_message = "Failed : <br> Password not updated";
}} else {
$error_message = "Password not matched";
} } ?>
<!DOCTYPE html>
<html>
<head>
<title>Reset Password</title>
<link rel="stylesheet" type="text/css" href="styless.css">
</head>
<body>
<br><br><div id="sidebar-left"></div>
<div class="reset"> Reset password </div><br><br>
<form id="reserPassword" action="new_pass.php" name="reserPassword" method="post">
<br><br><br><br><br><br><br><br><br><br>
<div id="borr"> Reset Password<br>
<?php if(!empty($success_message)) { ?>
<?php echo $success_message ?>
<?php } ?>
<?php if(!empty($error_message)) { ?>
<?php echo $error_message ?>
<?php } ?>
<br> <input type="password" id="password" name="password" placeholder="Enter a New Password" required>
<br> <input type="password" id="confirmPassword" name="confirmPassword" placeholder="Confirm Password" required>
<br><input type="submit" value="Reset Password" name="reset-password" id="forget-password"> </form></div></body></html>
【问题讨论】:
-
您是否尝试在逻辑位置上使用
var_dump($var); exit();进行调试,以查看代码因可能的错误值而停止正常工作的位置? -
“程序将创建一个随机密码” - 上面的代码中没有任何内容可以“生成”任何密码。不过,您确实对密码进行了哈希处理(这很好)。如果这就是你所指的,那每次都会给你一个新的哈希值,但这是应该的。
-
感谢您的回答,但是.. 我希望当我输入新密码时,它将按原样保存在数据库中,发生在我身上的是程序将保存另一个随机密码。跨度>
标签: php mysql reset-password