Why should global variables not be relied upon as a data source in PHP sessions?

Global variables should not be relied upon as a data source in PHP sessions because they are not secure and can be easily manipulated by users. Instead, it is recommended to store session data in the $_SESSION superglobal array, which is specifically designed for managing session data securely.

// Start the session
session_start();

// Store data in the session
$_SESSION['username'] = 'JohnDoe';
$_SESSION['email'] = 'johndoe@example.com';

// Retrieve data from the session
$username = $_SESSION['username'];
$email = $_SESSION['email'];

// Destroy the session
session_destroy();