简体   繁体   English

在数据库中存储后,password_verify将不起作用

[英]password_verify won't work after storing in database

I have been searching and reading for about 50 posts about my problem, but still can't figure out my problem, so I am here. 我一直在搜索和阅读关于我的问题的大约50个帖子,但仍然无法弄清楚我的问题,所以我在这里。

Problem: password_verify() returns false even if password is correct. 问题:即使密码正确, password_verify()也会返回false。

This is how I store password into db(When register and edit profile) 这是我将密码存储到db中的方式(当注册和编辑配置文件时)

$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

And my latest password_verify() (I have tried many examples, no one works and now I'm sitting here with still not working code) 我的最新password_verify() (我尝试了很多例子,没有人工作,现在我坐在这里还没有工作代码)

public function doLogin($username,$password){
    try{

        $stmt = $this->conn->prepare("SELECT id, username, password FROM users WHERE username=:username");
        $stmt->execute(array(':username'=>$username));
        $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

        if (password_verify($password, $userRow['password'])) {
            $_SESSION['user_session'] = $userRow['id'];
            $_SESSION["result"]='You have succesfully logged in your profile!';
            return true;
        }else{
            return false;
        }

    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
}

My database field is 25 characters long. 我的数据库字段长度为25个字符。 What is wrong here? 这有什么不对?

As per the manual for password_hash() 根据password_hash()的手册

[...] Therefore, it is recommended to store the result in a database column that can expand beyond 60 characters (255 characters would be a good choice) [...]因此,建议将结果存储在数据库列中,该列可以扩展到超过60个字符(255个字符将是一个不错的选择)

That means that password_verify() will silently fail if you have a column that has length of 59 or less. 这意味着如果您的列长度为59或更短,则password_verify()将无提示失败。 Because MySQL will truncate the the hashed string when you insert the hashed password without saying anything. 因为MySQL会在您插入散列密码时截断散列字符串而不说任何内容。

The solution is to set your password column to length of 60 or higher - the manual suggests setting it to 255, so just do that. 解决方案是将password列设置为60或更高的长度 - 手册建议将其设置为255,所以就这样做。

Any passwords already stored won't have their hashes fixed, so they need to be updated or re-inserted. 已存储的任何密码都不会修复哈希值,因此需要更新或重新插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM