What are common pitfalls when accessing session variables in PHP scripts?

Common pitfalls when accessing session variables in PHP scripts include not starting the session with session_start() before accessing session variables, not checking if the session variable exists before trying to access it, and not sanitizing the data retrieved from session variables to prevent security vulnerabilities.

// Start the session before accessing session variables
session_start();

// Check if the session variable exists before accessing it
if(isset($_SESSION['username'])){
    $username = $_SESSION['username'];
}

// Sanitize the data retrieved from session variables
$sanitizedUsername = htmlspecialchars($_SESSION['username']);