【发布时间】:2023-03-23 09:09:02
【问题描述】:
我想使用 php 重置用户密码。我从 html 表单中获得了用户的当前密码和新密码。这是重置密码的php脚本。但即使用户输入正确的密码,它也总是执行else 部分。怎么样?任何解决方案?我知道可能有一个简单的错误,但我是新手,找不到任何错误。
$uid = $_SESSION['uid'];
$current_pass = $_POST['org_pass'];
$new_pass = $_POST['new_pass'];
if(isset($_POST['submit']))
{
$act_pass = $db_con->prepare("SELECT password FROM user WHERE u_id= ?");
$act_pass->bindParam(1,$uid);
$act_pass->execute();
$actual_pass = $act_pass->fetchColumn();
define('SALT', 'flyingrabbit');
$typed_pass = md5(SALT.$actual_pass);
if ($typed_pass == $current_pass)
{
$new_pass1 = md5(SALT . $new_pass);
$res = $db_con->prepare("UPDATE user SET password= ? WHERE u_id=?");
$res->bindParam(1,$new_pass1);
$res->bindParam(2,$uid);
$res->execute();
header("Location: profile.php");
exit;
}
else
{
echo "<script type=\"text/javascript\">window.alert(\"You entered wrong password.\");window.location.href = 'profile.php';</script>";
}
}
【问题讨论】:
-
您的意思是将$current_pass 替换为$actual_pass 吗?
-
我想用
$new_pass替换$actual_pass。$current_pass..user 以 html 形式输入,这是他的密码。$actual_pass是存储在数据库中的用户密码。 -
您指定: if ($typed_pass == $current_pass) 您是否 100% 确定这是您想要的?在我看来,您应该这样做: if ($typed_pass == $actual_pass) 对于 typed_pass,您将数据库中的密码转换为哈希。您应该对输入执行此操作,而不是数据库中的内容。
-
这里有很多问题。首先,您只是假设您正在获得一排回来。你的盐是一个常数,这是不安全的,你使用的 md5 也是不安全的。最后成功和失败的登录有效地做同样的事情:带你到 profile.php 而不启动会话或设置任何类型的 var。
-
@jeroen 好点;在这种情况下,我会将 OP 转至 stackoverflow.com/questions/19103340/…