php - 哎呀 PHP登录检查散列密码
问题描述
我的问题是在课堂上检查散列密码。这是错误:
致命错误:未捕获的错误:无法在 Auth.php:51 中使用 PDOStatement 类型的对象作为数组 堆栈跟踪:#0 index.php(19): Auth->login() #1 {main} 在 Auth.php 中抛出在线51
if(isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
#LINE 19
$ui->login($username, $password);
}
这是我的代码
public function login ( $username, $password ) {
$u_login = $this->conn->prepare("SELECT * FROM korisnici WHERE username = :username");
$u_login->bindValue(':username', $username);
$u_login->execute();
$u_login->fetch(PDO::FETCH_ASSOC);
# LINE 51
$u_v_PASS = password_verify($password, $u_login['password']);
if($u_v_PASS->count()) {
echo 'uspesno';
} else {
}
}
解决方案
您正在使用 PDOStatement 对象执行提取,但不存储该提取的结果。相反,您正在 PDOStatement 对象本身中查找密码。尝试:
public function login ( $username, $password ) {
$u_login = $this->conn->prepare("SELECT * FROM korisnici WHERE username = :username");
$u_login->bindValue(':username', $username);
$u_login->execute();
/*
* Now that the prepared statement has been built and executed, we can
* try to fetch a matching user and store it as $user - this will be an
* array if successful or boolean false if not.
*/
$user = $u_login->fetch(PDO::FETCH_ASSOC);
/* Check that $user is not false and the password_verify returns boolean
* true when comparing the password to the hashed password stored in the
* database.
*/
if ($user && password_verify($password, $user['password'])) {
// User was found and password matched
}
}
推荐阅读
- angular - 如何根据数据库表检查 Ionic 3 Angular 应用程序中的有效促销代码?
- sql - Merge rows in postgresql
- docker - 通过命令行为 docker compose 创建自定义网络
- java - Spring Security 允许每个用户查看他们自己的个人资料,但没有其他人
- android - WorkManager:无法访问主线程上的数据库,因为它可能会长时间锁定 UI
- react-native - 在 React Native App 中使用多个包的优缺点
- javascript - 带参数的 AlaSQL 用户定义函数
- javascript - 更改路由器位置搜索时反应更新组件
- javascript - 使用 typescript react redux 设置 reducer
- excel - VBA 从多个工作表中获取双倍值