Whats the proper way to use password_verify with PDO?

前端 未结 2 1301
南方客
南方客 2021-01-21 16:54

I can\'t seem to get password_verify to work w/in my php PDO code. My pass field is stored as varchar(255). I\'ve been reading similar questions, but from what I can tell I hav

2条回答
  •  忘掉有多难
    2021-01-21 17:11

    The arguments for password_verify() are (1) the unhashed password you want to check and (2) the hashed password you are using as a reference. You are hashing the first argument before comparing:

    $pass = trim($_POST['pass'];
    $passH = password_hash($pass, PASSWORD_DEFAULT);
    // ...
    if(count($check_user)>0 && password_verify($passH, $check_user['pass'])) {
    

    You should be doing password_verify($pass /** the unhashed one */, $check_user['pass'])

    Also, trimming the password is a bad idea. What if the password actually includes whitespace (which you should allow it to do)?

提交回复
热议问题