php 验证密码不能为空,php – 密码未使用函数password_verify验证

我想我直接从mysql数据库使用函数PASSWORD散列密码(我在这里做错了吗?).我试图用这段代码验证密码:

if($submit)

{

$first=$_POST['first'];

$password=$_POST['password'];

$hash="*85955899FF0A8CDC2CC36745267ABA38EAD1D28"; //this is the hashed password i got by using function PASSWORD in database

$password=password_verify($password,$hash);

$db = new mysqli("localhost", "root","","learndb");

$sql = "select * from admin where username = '" . $first . "' and password = '". $password . "'";

$result = $db->query($sql);

$result=mysqli_num_rows($result);

if($result>0)

{

session_start();

$_SESSION['logged_in'] = true;

session_regenerate_id(true);

header("Location:loginhome.php");

}

}

但密码不匹配.我在这里失踪了什么?

更新:

完成所有建议之后我用php代码中的password_hash来存储到数据库中.

$db = new mysqli("localhost", "root","","learndb");

$password=password_hash('ChRisJoRdAn123',PASSWORD_DEFAULT);

$sql="INSERT INTO admin (username,password)values('ChrisJordan','$password')";

$db->query($sql);

密码仍然不匹配.

解决方法:

无法在数据库中搜索salted密码哈希.要计算哈希值,您需要使用password_hash()函数,就像在insert语句中已正确执行的那样.

// Hash a new password for storing in the database.

// The function automatically generates a cryptographically safe salt.

$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);

要检查密码,首先需要按用户名搜索(使用准备好的查询来避免sql注入):

$sql = 'select * from admin where username = ?';

$db->prepare($sql);

$db->bind_param('s', $first);

当您最终从数据库中获取存储的哈希时,可以像这样检查它:

// Check if the hash of the entered login password, matches the stored hash.

// The salt and the cost factor will be extracted from $existingHashFromDb.

$isPasswordCorrect = password_verify($password, $existingHashFromDb);

标签:php,mysql,passwords,database,mysqli

来源: https://codeday.me/bug/20190926/1820226.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值