在 PDO 中使用 password_verify 的正确方法是什么?
Whats the proper way to use password_verify with PDO?
我似乎无法让 password_verify 工作 w/in 我的 php PDO 代码。我的通行证字段存储为 varchar(255)。我一直在阅读类似的问题,但据我所知,我的设置是正确的。我一定还遗漏了什么。
我的注册页面如下..
$user = $_POST['username']
$pass = $_POST['pass'];
$passH = password_hash($pass, PASSWORD_DEFAULT);
$query = $con->prepare("INSERT INTO emps (user, pass) VALUES (?, ?)");
$query->execute([$user, $passH]);
加密密码现已成功存储在我的数据库中。
我的登录页面如下..
if(isset($_POST['login'])) {
$username = $_POST['username'];
$pass = trim($_POST['pass'];
$passH = password_hash($pass, PASSWORD_DEFAULT);
$sel_user = $con->prepare("SELECT id, username, pass, gid FROM emps WHERE gid!=4 AND username=?");
$sel_user->execute([$username]);
$check_user=$sel_user->fetch();
if(count($check_user)>0 && password_verify($passH, $check_user['pass'])) {
$_SESSION['username']=$check_user['username'];
header("Location: xadmin.php");
exit;
}
else {
echo "<script>alert('Not Found')</script>";
登录页面正文..
<form action="login.php" method="post">
<table width="100%" border="0">
<tbody>
<tr>
<td bgcolor="#3B3B3B" height ="35" class="BodyTxtB" align="center">Administrator Login</td></tr>
<tr height="20"><td></td></tr>
<tr>
<td class="BodyTxtB" align="center">Username</td>
</tr>
<tr>
<td class="BodyTxtB" align="center"><input type="text" class="BodyTxtBC" name="username" required="required"/></td>
</tr>
<tr height="20"><td></td></tr>
<tr>
<td class="BodyTxtB" align="center">Password</td>
</tr>
<tr>
<td class="BodyTxtB" align="center"><input type="password" class="BodyTxtBC" name="pass" required="required"/></td>
</tr>
<tr height="20"><td></td></tr>
<tr height="35"><td align="center"><input type="image" src="images/btn_login.jpg" name="login" value="Login"/>
<input type="hidden" name="login" value="Login" /></td></tr>
<tr height="20"><td></td></tr>
</tbody>
</table>
</form>
任何人都可以发现任何错误吗?
RTM? http://php.net/password_verify
boolean password_verify ( string $password , string $hash )
您为 $password
输入 PLAINTEXT 密码。你自己不散列它。这只会生成一个带有不同盐的新散列,使比较变得毫无意义和不可能。
password_verify
将从 $hash
中提取适当的盐,用它来散列 $password
本身,然后比较散列字符串。
例如password_verify 基本上就是这样:
function password_verify($pw, $hash) {
$salt = get_salt_from($hash);
$temp = password_hash($pw, $salt);
return ($temp == $hash);
}
password_verify()
的参数是 (1) 您要检查的未散列密码和 (2) 您用作参考的散列密码。您在比较之前对第一个参数进行哈希处理:
$pass = trim($_POST['pass'];
$passH = password_hash($pass, PASSWORD_DEFAULT);
// ...
if(count($check_user)>0 && password_verify($passH, $check_user['pass'])) {
你应该password_verify($pass /** the unhashed one */, $check_user['pass'])
另外,修改密码不是个好主意。如果密码实际上包含空格(您应该允许它这样做)怎么办?
我似乎无法让 password_verify 工作 w/in 我的 php PDO 代码。我的通行证字段存储为 varchar(255)。我一直在阅读类似的问题,但据我所知,我的设置是正确的。我一定还遗漏了什么。 我的注册页面如下..
$user = $_POST['username']
$pass = $_POST['pass'];
$passH = password_hash($pass, PASSWORD_DEFAULT);
$query = $con->prepare("INSERT INTO emps (user, pass) VALUES (?, ?)");
$query->execute([$user, $passH]);
加密密码现已成功存储在我的数据库中。
我的登录页面如下..
if(isset($_POST['login'])) {
$username = $_POST['username'];
$pass = trim($_POST['pass'];
$passH = password_hash($pass, PASSWORD_DEFAULT);
$sel_user = $con->prepare("SELECT id, username, pass, gid FROM emps WHERE gid!=4 AND username=?");
$sel_user->execute([$username]);
$check_user=$sel_user->fetch();
if(count($check_user)>0 && password_verify($passH, $check_user['pass'])) {
$_SESSION['username']=$check_user['username'];
header("Location: xadmin.php");
exit;
}
else {
echo "<script>alert('Not Found')</script>";
登录页面正文..
<form action="login.php" method="post">
<table width="100%" border="0">
<tbody>
<tr>
<td bgcolor="#3B3B3B" height ="35" class="BodyTxtB" align="center">Administrator Login</td></tr>
<tr height="20"><td></td></tr>
<tr>
<td class="BodyTxtB" align="center">Username</td>
</tr>
<tr>
<td class="BodyTxtB" align="center"><input type="text" class="BodyTxtBC" name="username" required="required"/></td>
</tr>
<tr height="20"><td></td></tr>
<tr>
<td class="BodyTxtB" align="center">Password</td>
</tr>
<tr>
<td class="BodyTxtB" align="center"><input type="password" class="BodyTxtBC" name="pass" required="required"/></td>
</tr>
<tr height="20"><td></td></tr>
<tr height="35"><td align="center"><input type="image" src="images/btn_login.jpg" name="login" value="Login"/>
<input type="hidden" name="login" value="Login" /></td></tr>
<tr height="20"><td></td></tr>
</tbody>
</table>
</form>
任何人都可以发现任何错误吗?
RTM? http://php.net/password_verify
boolean password_verify ( string $password , string $hash )
您为 $password
输入 PLAINTEXT 密码。你自己不散列它。这只会生成一个带有不同盐的新散列,使比较变得毫无意义和不可能。
password_verify
将从 $hash
中提取适当的盐,用它来散列 $password
本身,然后比较散列字符串。
例如password_verify 基本上就是这样:
function password_verify($pw, $hash) {
$salt = get_salt_from($hash);
$temp = password_hash($pw, $salt);
return ($temp == $hash);
}
password_verify()
的参数是 (1) 您要检查的未散列密码和 (2) 您用作参考的散列密码。您在比较之前对第一个参数进行哈希处理:
$pass = trim($_POST['pass'];
$passH = password_hash($pass, PASSWORD_DEFAULT);
// ...
if(count($check_user)>0 && password_verify($passH, $check_user['pass'])) {
你应该password_verify($pass /** the unhashed one */, $check_user['pass'])
另外,修改密码不是个好主意。如果密码实际上包含空格(您应该允许它这样做)怎么办?