UPDATE: I just want to update to share how I finally solved the issue I was having with the password_hash function in case others run into the same problem. I did not have the size of my password table long enough to accommodate the size of the hashed password, which worked fine for MD5. After changing the password table to 255 characters, these functions worked how they should.
I am adding users to my database and trying to use password_hash with the password they submit. Once added, users need to be able to log in. I am trying to verify the password with verify_password but it keeps coming back false.
I have been able to add users with password_hash but unable to log in with added users using the password_verify method. Strangely, if I add a user with MD5 within phpmyadmin, I can log them in no problem using md5($password). If I add users with md5 through my code, even if the passwords match, I am unable to log them in with this method.
I have been searching and searching and can't seem to figure out what I am doing wrong.
This was meant as a test to see if I can log in users. I know MD5 is not a great way. If I manually add a user and use the MD5 method in phpmyadmin, this code works:
if(md5($password) === $hashed) {
// log in user code
}
When adding the password, I am encrypting it like this:
$password = md5($_POST['password']);
Then I add it to an array and insert it into the database. Once again, this is just a test.
When I try using the password_hash function, which is what I would like to do, password_verify does not work. This is what I am trying to do.
INSERT FUNCTION:
function insertAgent($conn) {
$firstname = testdata($_POST['firstname']);
$middlename = testdata($_POST['middlename']);
$lastname = testdata($_POST['lastname']);
$phone = testdata($_POST['phone']);
$email = testdata($_POST['email']);
$position = testdata($_POST['position']);
$agency = testdata($_POST['agency']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$agentArray[] = array(
'AgentId' => '',
'AgtFirstName' => $firstname,
'AgtMiddleInitial' => $middlename,
'AgtLastName' => $lastname,
'AgtBusPhone' => $phone,
'AgtEmail' => $email,
'AgtPosition' => $position,
'AgencyId' => $agency,
'password' => $password
);
foreach ($agentArray as $array) {
$query = "INSERT INTO agents";
$result = $conn->query($query);
$query .= " (`".implode("`, `", array_keys($agentArray[0]))."`) VALUES";
foreach ($agentArray as $array) {
$query .= " ('".implode("', '", $array)."'),";
}
$query = substr($query,0,-1); // remove last comma
$result = mysqli_query($conn, $query) or die(mysql_error());
LOGIN:
if(!empty($_POST["login"])) {
$useremail = trim($_POST['useremail']);
$password = trim($_POST['password']);
$pass_query = mysqli_query($conn, "SELECT password FROM agents WHERE AgtEmail='$useremail'");
$pass = mysqli_fetch_assoc($pass_query);
$hashed = $pass['password'];
if(password_verify($password, $hashed)) {
$result = mysqli_query($conn, "SELECT * FROM agents WHERE AgtEmail='" . $useremail . "'");
$row = mysqli_fetch_assoc($result);
if(is_array($row)) {
$_SESSION["AgentId"] = $row['AgentId'];
}
} else {
$message = "<p class='errorForm'>Invalid Email or Password</p>";
}
}
The hashed version in the DB ($hashed) matches the md5 version from the user but it results in false unless I manually add it.