What are common pitfalls in storing user login data in PHP sessions?
One common pitfall in storing user login data in PHP sessions is not properly sanitizing and validating the input data before storing it in the session. This can lead to security vulnerabilities such as session hijacking or injection attacks. To solve this issue, always sanitize and validate user input before storing it in the session to ensure data integrity and security.
// Sanitize and validate user input before storing in session
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
// Validate username and password
if (!empty($username) && !empty($password)) {
// Store user login data in session
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
} else {
// Handle invalid input
echo "Invalid username or password.";
}