What best practices should be followed when handling sensitive data like passwords in PHP sessions?
Sensitive data like passwords should never be stored directly in PHP sessions as they can be easily accessed by malicious users. Instead, passwords should be securely hashed before being stored in the session. When retrieving the password from the session, it should be compared to the hashed version for verification.
// Store hashed password in session
$_SESSION['hashed_password'] = password_hash($password, PASSWORD_DEFAULT);
// Verify password from session
if (password_verify($input_password, $_SESSION['hashed_password'])) {
// Password is correct
} else {
// Password is incorrect
}