Fetch the stored hash for the account, then use password_verify(). Do not hash the submitted password again and compare strings.
<?php
$submittedPassword = $_POST['password'] ?? '';
$storedHash = $user['password_hash'] ?? '';
if ($storedHash !== '' && password_verify($submittedPassword, $storedHash)) {
echo 'Password accepted';
} else {
echo 'Invalid password';
}
?>$str="plus2net.com";
$str_hash=password_hash($str,PASSWORD_DEFAULT);
//echo $str_hash;
if(password_verify($str,$str_hash)){
echo " Matching ";
}else{
echo " Not matching ";
}
Output is
Matching
Syntax
password_verify('$entered_string',$hash_string);
| Parameter | DESCRIPTION |
|---|---|
| $entered_string | Required : Input password string ( user entered ) |
| $hash_string | Required : Hash created by password_hash(). |
$str="plus2net.com";
$str_crypt_hash = crypt($str);
///////////
if(password_verify('plus2net.com',$str_crypt_hash)){
echo " Matching ";
}else{
echo " Not matching ";
}
Output is
Matching
$password = "secure123";
$hash = password_hash($password, PASSWORD_DEFAULT);
if (password_verify("secure123", $hash)) {
echo "Password is correct!";
} else {
echo "Password is incorrect!";
}
$password = "secure123";
$hash = password_hash($password, PASSWORD_DEFAULT);
if (password_verify("wrong_password", $hash)) {
echo "Password is correct!";
} else {
echo "Password is incorrect!";
}
$hash = password_hash("mypassword", PASSWORD_DEFAULT);
if (password_verify("mypassword", $hash)) {
if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
$newHash = password_hash("mypassword", PASSWORD_DEFAULT);
echo "Password rehashed!";
}
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.