What are the best practices for handling session variables in PHP, especially in relation to session_start() and $_SESSION usage?

Session variables in PHP should be handled securely to prevent session hijacking or data manipulation. It is essential to call session_start() at the beginning of each script that uses session variables and to properly sanitize and validate any data stored in $_SESSION. Additionally, using HTTPS and setting secure and HttpOnly flags for session cookies can enhance security.

<?php
// Start the session
session_start();

// Set a session variable
$_SESSION['username'] = 'example_user';

// Retrieve and use the session variable
$username = $_SESSION['username'];

// Unset a session variable
unset($_SESSION['username']);

// Destroy the session
session_destroy();
?>