试图验证散列密码和验证登录pdo php
你好,我尝试使用pdo从数据库返回散列密码,并允许用户登录,而他们的密码被保护。我已经建立了一个注册页面,其中输入密码。我现在试图去解决这个问题。我认为我很接近,但这已经有很多日子停留在同一个地方,我希望有人能帮我完成我的代码?试图验证散列密码和验证登录pdo php
PHP致命错误:未捕获错误:调用成员函数fetch()布尔值在/var/www/html/login.php:54\nStack trace:\ n#0 {main} \ n抛出/无功/ www/html等/ login.php中上线54,引用者:http://172.16.62.211/login.php
if (isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
$q = $handler->prepare('SELECT * FROM users WHERE username = ?');
$query = $q->execute(array(
$username));
$_SESSION['username'] = $username;
$count = $q->rowCount();
if ($count == 1 -> fetch(PDO::FETCH_ASSOC)){
$hash_pwd = $count['password'];
$hash = password_verify($password, $hash_pwd);
if ($hash_pwd == 0) {
header("location: login.php?error=empty");
}
else {
$q = $handler->prepare('SELECT * FROM users WHERE username = ? && password = $hash_pwd');
$query = $q->execute(array(
$username,
$password));
$count = $q->rowCount();
if($count == 1){
$_SESSION['username'] = $username;
header("location: index.php");
return;
} else {
echo '<p class="error-message3">'.'<br>'.'<br>'."You have ented an incorrecct login!<br>Please try again.".'</p>';
}}}}
?>
你不会和你不能 'dehash' 的从你的数据库密码。
正常程序基本上由of converting the password inserted by the user to your security logic encrypt and compare with the password stored in your db (hashed too)组成,这两个值必须等于验证。
Dehash的密码是,如果哈希正确完成,在某些方面是不可能的,而不是指出你的想法的安全漏洞。
if ($count == 1 -> fetch(PDO::FETCH_ASSOC)){
$hash_pwd = $count['password'];
$hash = password_verify($password, $hash_pwd);
if ($hash === false) {
header("location: login.php?error=empty");
} else {
$_SESSION['username'] = $username;
header("location: index.php");
return;
}
} else {
echo '<p class="error-message3">'.'<br>'.'<br>'."You have ented an incorrecct login!<br>Please try again.".'</p>';
}
PS:缩进更好的代码,并指出了一些程序,你正在重复检查,你已经签像上面说的password_verify。
好吧,伙计们,抱歉,我明白我无法排除,但是如何验证密码? –
你看看'password_verify()'是否返回了真@diced。菠萝 –
我已经在问题中说过:您在登录时转换用户插入的密码并在您的数据库中进行比较 – calexandre
您需要了解的password_verify
使用,它需要生(非散列)密码的版本和的散列版本(从数据库),并分别返回真/假。
<?php
if (password_verify($raw, $hash)) {
}
?>
你并不需要在你的查询比较,刚刚从所提供的用户名(如果用户名是有效的),然后验证找回密码。
向下选民,谨慎解释?如果我的回答有问题,请说。不要只是投票和离开。 – Script47
*“我现在正试图去解决这个问题。”* - 你究竟是什么意思?哪一条是第54行? –
您也只在查询中有1个占位符,但在数组中使用2作为'$ q = $ handler-> prepare('SELECT * FROM users WHERE username =?&& password = $ hash_pwd');'。这也是失败的另一个原因。 –
@ Fred-ii-我试图在我的数据库中散列一个哈希密码来启用登录。 –