在 PDO 中使用 password_verify 的正确方法是什么?

问题描述 投票:0回答:2

我似乎无法让 password_verify 在我的 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>

任何人都可以发现任何错误吗?

php mysql pdo password-encryption
2个回答
3
投票

password_verify()
的参数是 (1) 您要检查的未散列密码和 (2) 您用作参考的散列密码。在比较之前,您正在散列第一个参数。

相反,你应该做

password_verify($pass /** the unhashed one */, $check_user['pass'])

$stmt = $con->prepare("SELECT id, username, pass, gid FROM emps WHERE username=?");
$stmt->execute([$_POST['username']]);
$user = $stmt->fetch();
if ($user && password_verify($_POST['pass'], $check_user['pass'])) {
    $_SESSION['username']=$user['username'];
    header("Location: xadmin.php");
    exit;
}

另外,修改密码不是个好主意。如果密码实际上包含空格怎么办(您应该允许它这样做)?


1
投票

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.