Here is the code for inserting a new user on registration
$passwordHash = password_hash($password, PASSWORD_BCRYPT,ARRAY("COST"=> 12));
$result = add_user($firstname, $lastname, $email, $username, $passwordHash);
Here is the function:
function add_user($firstname, $lastname, $email, $username, $password)
{
global $db;
try
{
$sql = "
INSERT INTO users
(firstname, lastname, email, username, password, reg_date)
VALUES
(:firstname, :lastname, :email, :username, :password, NOW())";
$stmt = $db->prepare($sql);
$stmt->bindParam(':firstname', $firstname, PDO::PARAM_STR);
$stmt->bindParam(':lastname', $lastname, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->execute();
return true;
}
catch(Exception $e)
{
return false;
}
}
Here is the code for updating password:
$passwordHash = password_hash($password, PASSWORD_BCRYPT, ARRAY("COST"=> 12));
$result = update_user_password($passwordHash, $userEmail);
And here is the function:
function update_user_password($newpwdHash, $email)
{
global $db;
try
{
$sql = "UPDATE users
SET password =:newpwdHash
WHERE email=:email";
$stmt = $db->prepare($sql);
$stmt->bindParam(':newpwdHash', $newpwdHash, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->execute();
return true;
}
catch(Exception $e)
{
return false;
}
}// End function
After updating password, on login I get an error, incorrect username / password
Where is the problem if I hash the password exactly the same way ?