简体   繁体   English

PHP password_verify对数据库不起作用

[英]PHP password_verify not working against database

I'm trying to me a page more secure and I started with the password encrypting part of it. 我正在尝试为我提供一个更安全的页面,我从页面的密码加密部分开始。 I'm trying to implement password_hash + password verify, but so far I've been unsuccessful to make the whole thing work. 我正在尝试实现password_hash +密码验证,但是到目前为止,我无法使整个过程正常进行。 So, here it is in my login area: 因此,这里是我的登录区域:

$username = mysqli_real_escape_string($connection, $_POST['username']);

$password = mysqli_real_escape_string($connection, $_POST['password']);

$query = "SELECT username, password FROM `users` WHERE username='$username' and user_enabled='1'";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
if($row = mysqli_fetch_assoc($result)) { $dbpassword = $row['password']; }

if(password_verify($password, $dbpassword)) {
    echo "Successful login";
}else{
    echo "Invalid Login Credentials.";
}

I always get Invalid Login Credentials. 我总是收到无效的登录凭据。

When I modify the new password for the user, I am doing the following: 当我为用户修改新密码时,我正在执行以下操作:

$pass = mysqli_real_escape_string($connection, $_POST['password']);
$options = [ 'cost' => 10,
             'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
           ];
$password = password_hash($pass,  PASSWORD_BCRYPT, $options)."\n";

$query = "UPDATE users 
          SET `password` = '".$password."'
          WHERE id = ".$_POST['user_id']."
          ";

$result = mysqli_query($connection, $query) or die(mysqli_error($connection));

password in database is VARCHAR(255), and it is storing something like: 数据库中的密码为VARCHAR(255),并且存储如下内容:

$2y$10$Y5HIyAsLMfkXIFSJONPsfO3Gxx3b46H.8/WFdLVH3Fqk2XNfy2Uaq

What am I doing wrong here? 我在这里做错了什么?

The \\n in the following line, is embedding a linebreak, (Edit: one that cannot be included in the user inputted password) . 下一行的\\n嵌入了一个换行符, (编辑:用户输入的密码中不能包含的换行符

$password = password_hash($pass,  PASSWORD_BCRYPT, $options)."\n";

and you need to delete it and start over with a new hash. 您需要将其删除并以新的哈希值重新开始。

Jay Blanchard , a member here on Stack submitted a note about it not too long also in the password_hash() manual, which is something that he and I actually talked about. Jay Blanchard ,Stack上的一名成员也在password_hash()手册中不久也提交了关于它的注释 ,这是他和我实际上谈到的。

Be care when using the example from the documentation which concatenates a newline character \\n to the end of the hash, ie: 使用文档中的示例时要小心,该示例将换行符\\n到哈希的末尾,即:

echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\\n";

People are storing the hash with the concatenated newline and consequently password_verify() will fail. 人们使用连接的换行符存储哈希,因此password_verify()将失败。

Another option would be to use trim() ; 另一种选择是使用trim() ; that also works (at the moment of hashing). 这也起作用(在哈希的时刻)。

$password = password_hash($pass,  PASSWORD_BCRYPT, $options)."\n";
$password = trim($password);
// Store in db after

Yet you still need to start over by clearing the old hash(es) and creating new ones. 但是,您仍然需要清除旧的哈希并创建新的哈希来重新开始。

Do keep in mind though, that you shouldn't escape passwords. 不过请记住,不要逃避密码。

One such as 123'\\abc (being perfectly valid) will be modified to 123\\'\\abc by real_escape_string() ; 诸如123'\\abc (完全有效)之类的代码将由real_escape_string()修改为123\\'\\abc it's not needed. 不需要 password_verify() takes care of that, security-wise. password_verify()会在安全方面进行处理。

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

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