【发布时间】:2018-07-13 18:12:28
【问题描述】:
我正在测试新的 php 加密算法 (Argon2),当我从数据库收集数据时它给我带来了问题。我在下面显示代码,password_verify () 总是返回 false。
设置通行证:
function setpass($pass, $cryp){
global $conn;
$qry="UPDATE users SET pass=:pass WHERE cryp LIKE :cryp";
$result=$conn->prepare($qry);
$password=password_hash($pass, PASSWORD_ARGON2I);
$result->bindParam(':pass', $password);
$result->bindParam(':cryp', $cryp);
$result->execute();
header("Location: http://localhost/intranet/login.php");
}
登录:
function login($nick, $pass){
global $conn;
$qry="SELECT id, pass FROM users WHERE nick LIKE :nick";
$result=$conn->prepare($qry);
$result->bindParam(':nick', $nick);
$result->execute();
$user=$result->fetch();
if(password_verify($pass, $user['pass'])){
setcookie("user_id", $user['id'], time()+432000);
setcookie("user_nick", $user['nick'], time()+432000);
header("Location: xxxx");
}
else{
var_dump("ERROR");
}
}
唯一失败的是password_verify 函数。哈希已很好地插入数据库中,如果我尝试在同一页面上使用测试字符串执行哈希和password_verify,如果它运行良好。编码就像数据库和我的.php中的utf-8
【问题讨论】:
-
您确定选择的是同一行吗?
setpass使用cryp列,login使用nick。 -
我们可以看一些我们可以运行的示例代码吗?一个不需要数据库的独立minimal reproducible example 是理想的。
-
确定
$result->fetch();会产生什么?还要检查$pass是否为空 -
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
是什么促使您将此归咎于特定的 PHP 版本?这是在其他任何东西上测试的吗?还是使用文字数据库内容和固定密码? (关于 DB 列定义的详细信息不足。)