What are the common pitfalls when handling sessions in PHP?

One common pitfall when handling sessions in PHP is not properly securing the session data, which can lead to security vulnerabilities. To solve this, you should always use HTTPS to encrypt the data during transmission and store sensitive data in the session securely.

// Start a secure session
session_start([
    'cookie_lifetime' => 86400, // 1 day
    'cookie_secure' => true, // only send cookie over HTTPS
    'cookie_httponly' => true, // prevent access from JavaScript
]);

// Store sensitive data securely in the session
$_SESSION['user_id'] = encrypt($user_id);