2016-04-17 30 views
0

我正在为我的db中的唯一用户写入登录代码。 但password_verify函数似乎不起作用。当我在echo这两个hashdbpassword写在窗体,我看到他们,所以没有问题的查询或$_POSTpassword_verify不起作用,即使它具有散列和用户密码

这里是我的代码: 日志中:提前

$passwordFromForm = htmlspecialchars($_POST['password']); 
$nmbr = 12; // it's the user's id. 
$sql = "SELECT * FROM user WHERE iduser = $nmbr"; 
$res = mysqli_query($conn, $sql); 
// $row = mysqli_fetch_assoc($res); 
while($row = $res->fetch_assoc()) { 
    $hashFromDB = $row['hash']; 
} 
if(password_verify($passwordFromForm, $hashFromDB)) { 
    echo "success"; 
    header("Location: ../admin.php"); 
} 
else { 
    echo "The hash is:" . $hashFromDB . "and the pass is:" . $passwordFromForm; 
    //this echoes the correct hash and string 
} 

感谢。

回答

1

功能password_verify()取决于与password_hash(),您可以检查算法password_hash()使用是否符合password_verify(下同),与password_get_info检查RES($哈希)

+0

谢谢! 如果password_verify的散列与password_hash的散列相同? 他们是一样的。 – TheTryHardz

+0

这是password_get_info: array(3){[“algo”] => int(1)[“algoName”] => string(6)“bcrypt”[“options”] => array(1){[ “cost”] => int(12)}} – TheTryHardz

1

你不应该这样做在将其输入到password_hash()/password_verify()函数之前,密码的任何转义。因此,请删除对htmlspecialchars()的调用,并确保您的数据库字段包含散列,其类型为varchar(255)

+0

感谢您的回答。 我已经尝试没有htmlspecialchars(),并且数据库是VARCHAR(255),但它仍然不起作用。 – TheTryHardz

+0

@TheTryHardz - 你能举一个'$ hashFromDB'内容的例子吗? – martinstoeckli

+0

在我的代码上,当我回显$ hashFromDB时,输出是散列$ 2y $ 10 $ oPHWLB.6Z0hBvjk8al24xuiG7Io4l7ERGCtLnW5Eij1wTlT67mLJW。 – TheTryHardz