What are common pitfalls to avoid when working with PHP sessions?

One common pitfall when working with PHP sessions is not properly starting the session before trying to access or set session variables. To avoid this issue, always start the session at the beginning of your PHP script using session_start(). Additionally, make sure to check if the session has already been started before trying to start it again.

<?php
// Start the session
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Access or set session variables
$_SESSION['username'] = 'john_doe';
?>