What are common pitfalls when using $_Session variables in PHP for navigation?

Common pitfalls when using $_SESSION variables for navigation in PHP include not properly checking if the session variable exists before using it, not properly sanitizing the input from the session variable, and not properly destroying the session after its use to prevent security risks.

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

// Sanitize the input from the session variable
$username = filter_var($_SESSION['username'], FILTER_SANITIZE_STRING);

// Destroy the session after its use
session_destroy();