What are the potential security risks associated with session handling in PHP?

One potential security risk associated with session handling in PHP is session fixation, where an attacker sets the session ID before the user logs in, allowing them to hijack the session. To prevent this, you can regenerate the session ID after a successful login.

// Start the session
session_start();

// Regenerate the session ID
session_regenerate_id(true);

// Set session variables after successful login
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = $user_id;