php - 无法使用散列密码登录 - Mysqli PHP
问题描述
我在注册页面上用下一个代码对我的密码进行了哈希处理:
$c_pass = $_POST['c_pass'];
$storePassword = password_hash($c_pass, PASSWORD_BCRYPT, array('cost' => 10));
在登录页面上我有这个代码:
$customer_email = $_POST['c_email'];
$customer_pass = $_POST['c_pass'];
$select_customer = "select * from customers where customer_email='$customer_email' AND customer_pass='$customer_pass'";
尝试登录时,他们会弹出一个错误屏幕,提示我的凭据无效。我尝试使用
if(password_verify($customer_password,$row['c_pass'])){
但是请注意帮助我,有人可以为此写我的解决方案,因为我找不到解决方案。
解决方案
您应该AND
从WHERE
子句中删除 ,因为当用户在登录表单输入中键入他/她的密码时,它将是非散列的,因此您永远不会通过复合条件电子邮件 + 密码检索用户。相反,您应该通过标识符(用户名或电子邮件)找到用户,然后您应该将来自登录表单密码输入的原始密码与来自数据库的哈希值进行比较。因此,请查看以下内容:
$mysqli = new mysqli('host', 'user', 'password', 'db_name');
// these are submitted input values from the login form which you handle
$email = $_POST['c_email'];
$password = $_POST['c_password'];
// build your query with prepared statements which will retrieve
// the user from the database with login form submitted email
$stmt = $mysqli->prepare('select * from customers where customer_email = ?');
$stmt->bind_param('s', $email);
$stmt->execute();
$result = $stmt->get_result();
$customer = $result->fetch_assoc();
if (empty($customer)) {
// no user found by this email, so send to user response message
// that your system cannot find any customer by provided email
return false;
}
// here you compare the typed user login password versus the fetched user's password from the database by the provided user email
if (password_verify($password, $customer['c_pass'])) {
// ok, so the password comparison match
} else {
// invalid password
}
推荐阅读
- firebase - Dialogflow:如何在 Firebase 查询中传递参数?
- python - 从 csv 文件导入数据 - 为什么他们打印不同的东西?
- c++ - 视觉工作室阿拉伯语文本:常量中的换行符
- python - Django - 使用通用视图找不到 URL
- python - Python:如何用 ast 重载运算符
- html - Node.js + Cheerio Scraping:如何在联系表中发帖?
- css - 忽略所有父 CSS 的 Html 元素
- android - CollapsingToolbarLayout 将工具栏文本推到屏幕外。
- python - visual studio code - 用 python 编写的语言服务器扩展
- angular - @handsontable/angular:如何使用热列创建自动完成或可动手操作的类型