What are the best practices for setting and accessing session variables in PHP for template changes?

When setting and accessing session variables in PHP for template changes, it is important to follow best practices to ensure security and efficiency. To set a session variable, use the $_SESSION superglobal array and assign the desired value to a specific key. To access the session variable, simply reference the key within the $_SESSION array. Make sure to sanitize and validate user input before storing it in session variables to prevent security vulnerabilities.

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

// Accessing a session variable
$user_id = $_SESSION['user_id'];