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

时间:2017-03-12 13:55:30

标签: php database pdo passwords verification

我收到此错误:

  

注意:未定义索引:密码日志在第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>";
    }

}

1 个答案:

答案 0 :(得分:1)

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

$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>';
    }
}
相关问题