PHP password_verify 从数据库中获取散列数据

PHP password_verify on fetched hashed data from database

我收到此错误:

Notice: Undefined index: passwordLog in line 60.

第 60 行:

if (password_verify($passwordLog,$row['password'])) {

我正在使用 PDO。

if (isset( $_POST['btnLog'])) {

    $usernameLog = $_POST['usernameLog'];
    $passwordLog = $_POST['passwordLog'];

    $selectQuery = $connection->prepare("SELECT * FROM ".$table." WHERE username = :usernameLog");
    $selectQuery->execute(array(':usernameLog' => $usernameLog));

    $row = $selectQuery->fetchAll();       

    if($selectQuery->rowCount() > 0 )
    { 
        if (password_verify($passwordLog,$row['passwordLog'])) {
                header('Location: sample.html');
        }
        else
        {
            echo '<script>alert("Invalid Username/Password");</script>';
        }
    }
    else
    {
        echo "<script>alert('Invalid Username or Password. Try again');</script>";
    }

}

好的,如果列名是固定的,那么下一个问题是你做了一个 fetchAll,其中 returns 一个行数组,因此对第一个结果行的引用将是

$row = $selectQuery->fetchAll();       

if($selectQuery->rowCount() > 0 )
{ 
    if (password_verify($passwordLog,$row[0]['password'])) {
                // code changed here     ^^^
        header('Location: sample.html');
    }
    else
    {
        echo '<script>alert("Invalid Username/Password");</script>';
    }
}

但是由于您只期望返回一行,或者至少您没有编码以期望返回多行,因此使用 ->fetch() 会更有意义,就像这样

$row = $selectQuery->fetch();       

if($selectQuery->rowCount() > 0 )
{ 
    if (password_verify($passwordLog,$row['password'])) {
            header('Location: sample.html');
    }
    else
    {
        echo '<script>alert("Invalid Username/Password");</script>';
    }
}