What are the best practices for passing data between pages using sessions in PHP?

When passing data between pages using sessions in PHP, it is important to securely store and retrieve the data to maintain user privacy and prevent data tampering. To achieve this, you can use the $_SESSION superglobal array to store and access data across different pages. Make sure to start the session at the beginning of each page where you want to access the session data.

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

// Store data in the session
$_SESSION['username'] = 'JohnDoe';

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

// Destroy the session when it is no longer needed
session_destroy();
?>