So I'm hitting my head against a brick wall here, I can't seem to figure out what I am doing wrong. I've looked everywhere and tried to implement all the answers but to no avail! I'm quite sure that it has something to do with the input into the SQL database, but I'm not sure what it is.
First, here is the code to input a users password from the form into the database. I and reasonably new to all this but I know about SQL injections (http://php.net/manual/en/security.database.sql-injection.php), and was planning on figuring that stuff out when I got this passwords working, and now its 4 hours later. Here's the database input code:
$user_id = mysqli_real_escape_string($mysqli,$_POST['user_id']);
$getpassword = mysqli_real_escape_string($mysqli,$_POST['password']);
$password = password_hash($getpassword, PASSWORD_DEFAULT);
$sql = "INSERT INTO `members` (`user_id`, `password`) VALUES ('$user_id', '$password')";
$insertRow = mysqli_query($mysqli, $sql);
if($insertRow){
print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
That inputs it into the database no problem. The password field of the database is a VARCHAR (225) with utf8_unicode_ci encoding.
On my login page (also index.php) here is the code to verify the password that has been entered:
if(isset($_POST['login']))
{
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$password = mysqli_real_escape_string($mysqli, $_POST['password']);
$res=mysqli_query($mysqli, "SELECT * FROM members WHERE user_id= '$username'");
$row=mysqli_fetch_assoc($res);
$hash = $row['password'];
$verified = password_verify($password, $hash);
if($verified)
{
$_SESSION['user'] = $row["user_id"];
echo "Verification succeeded";
} else
{
echo "Verification Failed";
}
}
It seems that not matter what I try it always returns "Verification Failed". When I cut and paste the hashed password that has been entered into the database, and run the code like this
$verified = password_verify($password, '$2y$10$QxFTdQZT7J2LqulNsRUWPO2LxBS5hmS3NeqLtEtQMEhtbIjoj6LNa');
it still returns "Verification Failed", which makes me suspect that it is something to do with what is happening as I am putting into the database.
I've check all these links and yet it seems that I must be doing something completely different (and most likely completely stupid and obvious):
php password_hash and password_verify looked all over still doesn't work
php password_verify not working with database
php password_verify not working
http://forums.phpfreaks.com/topic/283407-need-help-with-password-verify/
Anyway, its doing my head in, hopefully someone can see what (probably) obvious mistake that I have made.
Cheers