I am able to hash and verify a password like this
<?php
$password ="passss.com";
$hash = password_hash($password, PASSWORD_DEFAULT);
if (password_verify('passss.com', $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
?>
But when I tried to do same thing in the querying of data from MySQL I am always getting Invalid password
I simply created the $hash
in a PHP file and inserted it into MySQL table manually.
if (!isset($_POST['username'], $_POST['password'])) {
die('Please fill both the username and password field!');
}
if ($stmt = $con->prepare('SELECT id, userpassword FROM users WHERE useremail = ?')) {
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $userpassword);
$stmt->fetch();
if (password_verify($_POST['password'], $userpassword)) {
$_SESSION['loggedin'] = true;
echo "Password is valid";
} else {
echo "Invalid password.";
}
} else {
echo 'Incorrect username!';
}
}