EDIT: I guess the stars just fell out of alignment, because after reloading the page and trying to use it again it works.
I'm creating a simple administration system with user accounts. I'm having a hard time with the login bit, because for some reason password_verify
fails if it's in an if statement. It might be related to my method of getting the passwords out of the database, but I'm not sure.
here's what doesn't work
$query = $db->query("SELECT * FROM users WHERE `username` = '" . $db->real_escape_string($_POST['username']) . "';");
if (!$query or $query == NULL)
{
echo $db->error . "<br>";
exit("EXIT: database error");
}
while ($row = $query->fetch_assoc())
{
if (password_verify($_POST['password'], $row['password']))
{
$db->close();
header("LOCATION: main.php");
exit;
}
else
{
header("LOCATION: index.php?error=Wrong password.");
exit("wrong password<br>");
}
}
if I were to replace the line if (password_verify($_POST['password'], $row['password']))
with
$success = password_verify($_POST['password'], $row['password']);
if ($success)
// success
it works as expected. is there something wrong with me doing ($row = $query->fetch_assoc())
, or did I make a mistake somewhere else? I don't understand why it won't work in the IF statement. I'm sure I just did something stupid, but I cannot for the life of me find out what.
Would it be bad, or considered bad for me to use the latter method for password authentication?