I do have an older version of php which is 5.4.16 and I downloaded password.php from github
which makes the password_hash() works perfectly. I also inserted it into the database but when I try using the password_verify() I get invalid. I just want to pull out one row of data in the database just to create something for myself to log in.
my db is(db works fine because before I try to add a login I have a list which pulls data from the same database just different table and worked fine)
$db = new PDO('mysql:dbname=database;host=hostip', 'root', 'password');
I created this in php to insert into one row into my table
/*Inserts*/
$password = password_hash('asdf', PASSWORD_BCRYPT, array('cost' => 10));
$username = 'admin';
$insertQuery = $db->prepare("
INSERT INTO et_todo (username, password)
VALUES (:username, :password)
");
$insertQuery->execute(array(
'username' => $username,
'password' => $password
));
to try the verify I did this
/* Selects */
$selectQuery = $db->prepare("
SELECT id, username, password
FROM et_todo
WHERE id = :id
");
$selectQuery->execute(array(
'id' => 9 //should be 1 but 9 because I tried few testings
));
$rows = $selectQuery->rowCount() ? $selectQuery : array();
foreach ($rows as $row)
{
if (password_verify('asdf', $row['password'])) {
/* Valid */
echo 'valid';
} else {
/* Invalid */
echo 'invalid';
}
}
what I get in is invalid though. I couldn't figure out what I did wrong. Am I doing the query wrong?
P.S. If I ran the inserts few times the hashed password in database is different each time even if the password is always 'asdf' (which as mentioned below is normal but just trying to provide as much info as I can)
changed $selectQuery to $selectQuery->fetchAll(PDO::FETCH_ASSOC) so I took out the foreach loop and code as below but still getting invalid instead of valid though
$rows = $selectQuery->rowCount() ? $selectQuery->fetchAll(PDO::FETCH_ASSOC) : array();
echo $rows[0]['password'] . '<br>';
if (password_verify('asdf', $rows[0]['password'])) {
/* Valid */
echo 'valid';
} else {
/* Invalid */
echo 'invalid';
}