What are common pitfalls when using PHP sessions for user authentication?

One common pitfall when using PHP sessions for user authentication is not properly securing the session data, which can lead to session hijacking or session fixation attacks. To prevent this, it's important to use HTTPS to encrypt the data transmission and to regenerate the session ID after a successful login to prevent session fixation.

// Start a secure session
session_start([
    'cookie_secure' => true,
    'cookie_httponly' => true
]);

// Regenerate session ID after successful login
if ($authenticated) {
    session_regenerate_id(true);
}