How can PHP functions like passwordCheck() be used to verify user credentials before granting access?

To verify user credentials before granting access, you can create a PHP function like passwordCheck() that takes in the user's inputted password, compares it to the stored hashed password, and returns true if they match. This function can be called before allowing access to sensitive areas of your website.

// Function to check if the inputted password matches the stored hashed password
function passwordCheck($inputPassword, $storedHashedPassword) {
    return password_verify($inputPassword, $storedHashedPassword);
}

// Example of how to use the passwordCheck() function
$inputPassword = $_POST['password']; // Assuming password is submitted via a form
$storedHashedPassword = 'hashed_password_from_database'; // Get the hashed password from your database
if(passwordCheck($inputPassword, $storedHashedPassword)) {
    // Grant access to the user
    echo "Access granted!";
} else {
    // Deny access
    echo "Incorrect password. Access denied.";
}