What are common pitfalls when using sessions in PHP, and how can they be avoided?

One common pitfall when using sessions in PHP is not starting the session before trying to access or modify session variables. To avoid this, always call session_start() at the beginning of your script before working with session data.

<?php
session_start();

// Now you can access and modify session variables
$_SESSION['username'] = 'john_doe';
?>