What are the best practices for passing variables in PHP sessions?

When passing variables in PHP sessions, it is important to properly sanitize and validate the data to prevent security vulnerabilities such as session fixation attacks. It is recommended to use the $_SESSION superglobal array to store and retrieve session variables securely.

// Start the session
session_start();

// Set session variable
$_SESSION['user_id'] = 123;

// Retrieve session variable
$user_id = $_SESSION['user_id'];