What are the potential pitfalls of using SESSION variables for storing user information in PHP?
Using SESSION variables for storing user information in PHP can lead to potential security risks such as session hijacking or session fixation. To mitigate these risks, it is important to properly sanitize and validate user input before storing it in SESSION variables. Additionally, using HTTPS to encrypt the session data can help protect sensitive information.
// Start a secure session
session_start([
'cookie_lifetime' => 86400, // 1 day
'cookie_secure' => true, // only transmit cookie over HTTPS
'cookie_httponly' => true, // prevent client-side access to cookie
]);
// Sanitize and validate user input before storing in SESSION
$_SESSION['username'] = filter_var($_POST['username'], FILTER_SANITIZE_STRING);