简体   繁体   English

PHP无法在PDO查询中使用哈希密码

[英]PHP Can't use a hashed password in a PDO query

I'm currently a 3th year web-dev student, and for a school project we have to build a website with a change password function using PDO and hashes, however I have run into a problem I cannot solve on my own, I also haven't found any similar questions on both google and stackoverflow. 我目前是网络开发的三年级学生,对于一个学校项目,我们必须使用PDO和哈希值来构建一个具有更改密码功能的网站,但是我遇到了一个我自己无法解决的问题,在google和stackoverflow上都找不到任何类似的问题。

we have to store 2 passwords in the database, the old password (which is the password the user has lastly used); 我们必须在数据库中存储2个密码,即旧密码(这是用户最后使用的密码); and their current password (which will be used to login.) 及其当前密码(将用于登录)。

all of the passwords are stored with a standard PHP hash (the password_hash() method) 所有密码均使用标准PHP哈希存储(password_hash()方法)

When changing the user's password, I have to get the current password (the one used to login) from the database, and move it to the oldpassword (the last used password) column while putting the new hashed password into the database. 更改用户密码时,我必须从数据库中获取当前密码(用于登录的密码),然后将其移动到oldpassword(最后使用的密码)列,同时将新的哈希密码放入数据库中。

the problem is: I can't seem to use a hashed password in a PDO query, i do need to point out that I have never worked with PDO before, but after googling a bit I don't think I have an error in my query, but in the data being passed to that query. 问题是:我似乎无法在PDO查询中使用哈希密码,我需要指出的是,我以前从未使用过PDO,但是在进行了谷歌搜索之后,我认为我没有错误查询,但在传递给该查询的数据中。

this is where i get all the current data from the database 这是我从数据库中获取所有当前数据的地方

/*prepare and execute a query*/
$sqlStatement = $this->db->prepare("SELECT username, password, oldpassword FROM users WHERE username = :username");
$sqlStatement->bindParam(":username", $username, PDO::PARAM_STR);
$sqlStatement->execute();

/*Fetch the query results*/
$values = $sqlStatement->fetch(PDO::FETCH_ASSOC);

And this is where I try to store the data into the database using a PDO SQL query 这是我尝试使用PDO SQL查询将数据存储到数据库中的地方

/*Create variables to use in querys*/
$queryPassword = password_hash($password, PASSWORD_DEFAULT);

/*Update the passwords*/
$sqlStatement2 = $this->db->prepare("UPDATE password, oldPassword VALUES (:password, :oldpassword)");
$sqlStatement2->bindParam(":password", $queryPassword, PDO::PARAM_STR);
$sqlStatement2->bindParam(":oldpassword", $values["password"] , PDO::PARAM_STR);
$sqlStatement2->execute();

$password and $username are variables passed from a form, to the function to change the user's password. $ password和$ username是从表单传递到用于更改用户密码的函数的变量。

public function updateUserPasswords($username, $password){

What I find odd is: if i do a var_dump on $values["password"] I get the following data: 我发现奇怪的是:如果我对$ values [“ password”]进行var_dump,则会得到以下数据:
string(60) "$2y$10$BBCpJxgPa8K.iw9ZporxzuW2Lt478RPUV/JFvKRHKzJhIwGhd1tpa" 字符串(60)“ $ 2y $ 10 $ BBCpJxgPa8K.iw9ZporxzuW2Lt478RPUV / JFvKRHKzJhIwGhd1tpa”

Where my query error gives me the following data: 我的查询错误为我提供了以下数据:
$2y$10$ $ 2y $ 10 $

it's as if only the hash is being sent to the query. 就像只有哈希发送到查询一样。

==EDIT== ==编辑==

The SQL error being sent to the browser is: 发送到浏览器的SQL错误是:

Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES ('$2y$10$AC.aMG/gNV9zwGB/v/g7keW9jsZ80kuejrSh693DPuhOYChFxA6wu', '$2y$10$' at line 1' in /srv/jip/www/www.jip.nl/classes/user.class.php:92 Stack trace: #0 /srv/jip/www/www.jip.nl/classes/user.class.php(92): PDOStatement->execute() #1 /srv/jip/www/www.jip.nl/pages/first_login.php(26): User->updateUserPasswords('username', 'password') #2 {main} thrown in /srv/jip/www/www.jip.nl/classes/user.class.php on line 92

Don't retrieve the data using the hashed password, because password_hash() generates a new hash with a different salt whenever it's used 不要使用哈希密码来检索数据,因为password_hash()每次使用时都会生成带有不同盐值的新哈希

Retrieve the record using just the username, then use password_verify() to validate the password 仅使用用户名检索记录,然后使用password_verify()验证密码

EDIT 编辑

Your update query references columns, but no table, and without a WHERE clause: 您的更新查询引用的是列,但没有表,也没有WHERE子句:

$sqlStatement2 = $this->db->prepare("UPDATE users SET password=:password, oldPassword=:oldpassword WHERE username = :username");
$sqlStatement2->bindParam(":password", $queryPassword, PDO::PARAM_STR);
$sqlStatement2->bindParam(":oldpassword", $values["password"] , PDO::PARAM_STR);
$sqlStatement2->bindParam(":username", $username, PDO::PARAM_STR);
$sqlStatement2->execute();

binding with the appropriate values 绑定适当的值

Turned out my query was indeed incorrect, it's been a while since i last made an update query and I put it in the same format as an insert query. 原来我的查询确实不正确,距我上次进行更新查询并以与插入查询相同的格式进行查询已有一段时间了。

Mark Baker's solution also fixed the problem with retrieving the old password from the database. Mark Ba​​ker的解决方案还解决了从数据库中检索旧密码的问题。 retrieving the information using just the username has worked. 仅使用用户名检索信息已奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM