php - 密码验证无法将散列密码与用户输入的密码进行比较

标签 php mysql hash

我正在尝试将散列密码与用户输入的密码进行比较,但它不验证并说密码不正确。我使用此代码对密码进行哈希处理:$hashed_pa​​ssword = password_hash($me, PASSWORD_DEFAULT);

下面的代码是登录检查页面

host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbll_name="employees_table"; // Table name 
$tb2_name="system_users";
$tb3_name="managers_table";
// Connect to server and select databse.
$link = mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
mysqli_select_db($link,"$db_name")or die("cannot select DB");
$myusername=$_POST["myusername"];  
$mypassword=$_POST["mypassword"]; 

// To protect MySQL injection (more detail about MySQL injection )
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysqli_real_escape_string($link, $myusername);
$mypassword = mysqli_real_escape_string($link, $mypassword);


$statues = "active";

// from system table
if ($sql="SELECT * FROM $tb2_name WHERE User_id='".$myusername."' and statues='".$statues."'");
{

$wql="SELECT * FROM $tb2_name WHERE User_id='".$myusername."' ";
$result2 = mysqli_query($link, $wql);
$details = mysqli_fetch_array($result2, MYSQLI_BOTH);
$Name = $details["Company"];
$Surname = $details["First_name"];
$encrypted_password = $details["Password"];

上面的代码从数据库中获取散列函数,下面的代码比较它们。但它显示密码不正确,即使他们输入了正确的密码

if (password_verify($mypassword, $encrypted_password))
{ 

最佳答案

"i think the hashed password is greater than 30 characters so it just cut after 30. – Jerome"

您的查询“悄无声息”失败,因为密码列太短,您需要清除当前密码/散列并重新开始。

根据 password_hash() 函数的手册:

http://php.net/manual/en/function.password-hash.php

PASSWORD_DEFAULT - Use the bcrypt algorithm (default as of PHP 5.5.0). Note that this constant is designed to change over time as new and stronger algorithms are added to PHP. For that reason, the length of the result from using this identifier can change over time. 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).

关于php - 密码验证无法将散列密码与用户输入的密码进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41090555/

相关文章:

php - 使用 paypal iPN 向用户添加记录时出错

php - 使用 PHP 将 SMS AT 命令发送到 3G 调制解调器

MYSQL - 获取没有父对象的对象以及父对象中最后创建的对象

sql - 这个SQL怎么会错呢?我没看到什么?

当文件来自 system32 文件夹时,文件的哈希值 MD5 和 SHA256 会有所不同。为什么?

php - 无法保存/无法创建临时文件

php - 如何在 mPDF 中使用 bootstrap?

mysql - 用另一个计数更新表

c# - C#中的哈希密码?密码/PBKDF2

c# - 为什么 ClickOnce 下载未更改的文件?