What are some common pitfalls when working with PHP sessions, as seen in the provided code snippet?

One common pitfall when working with PHP sessions is not starting the session before trying to access or set session variables. To solve this issue, make sure to call session_start() at the beginning of your script before interacting with session variables.

<?php
session_start();

// Now you can access and set session variables
$_SESSION['username'] = 'JohnDoe';
echo $_SESSION['username'];
?>