简体   繁体   English

PHP - password_verify 总是返回 false

[英]PHP - password_verify always returning false

I'm having troubles with verifying a password with the password_hash and password_verify functions.我在使用 password_hash 和 password_verify 函数验证密码时遇到了麻烦。 For some reason it always returns false.出于某种原因,它总是返回 false。

The hash is stored in a database, when the user provides an email and a password, if a record of a user with the provided email exists, the provided pass and the hash from that user record is verified (which returns false, providing the right password).散列存储在数据库中,当用户提供电子邮件和密码时,如果存在提供电子邮件的用户记录,则验证提供的通行证和来自该用户记录的散列(返回 false,提供正确的密码)。

The code below is for test purposes because it wasn't working properly in the real context (with data stored in the database)下面的代码用于测试目的,因为它在实际上下文中无法正常工作(数据存储在数据库中)

Here's some of the code.这是一些代码。

  <?php
      //create random password with 15 chars
      $pass = generate_random_string(15);
      $hash = password_hash($pass, PASSWORD_BCRYPT);
      var_dump(password_verify($pass, $hash));
      //returns bool(true)

Until this part everything is fine, it creates a pass, hash it and when verified returns true.在这部分一切正常之前,它会创建一个通行证,对其进行哈希处理,并在验证后返回 true。 Now the weird part.现在是奇怪的部分。

       if (isset($_GET['pass']) &&
           isset($_GET['hash'])) {

            var_dump(password_verify($_GET['pass'], $_GET['hash']));
            //returns bool(false)
       }
   ?>

If I take the previous generated values (pass and hash) and pass them has URL parameters and verify them, it returns false.如果我采用先前生成的值(传递和散列)并传递它们具有 URL 参数并验证它们,则返回 false。

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

UPDATE更新

dumping $_GET array shows the correct parameters and values. dumping $_GET array 显示正确的参数和值。

Try encoding the values before appending them to the url:在将值附加到 url 之前尝试对其进行编码:

$pass = generate_random_string(15);
$hash = password_hash($pass, PASSWORD_BCRYPT);
var_dump(password_verify($pass, $hash));
$pass_encoded = urlencode($pass); // PASS THIS IN THE URL
$hash_encoded = urlencode($hash); // PASS THIS IN THE URL

And the decode it:和解码它:

if (isset($_GET['pass']) && isset($_GET['hash'])) {
    var_dump(password_verify(urldecode($_GET['pass']), urldecode($_GET['hash'])));
}

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

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