What is the best practice for managing session variables in PHP?

Session variables in PHP should be managed securely to prevent unauthorized access or tampering. It is recommended to use session_start() at the beginning of each PHP script to initialize the session. To set a session variable, use $_SESSION['variable_name'] = 'value'; and to retrieve a session variable, use $_SESSION['variable_name']. It is important to unset or destroy session variables when they are no longer needed to free up memory.

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

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

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

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

// Destroy the session
session_destroy();
?>