How can using $_SESSION as a superglobal variable improve security and functionality in PHP scripts?

Using $_SESSION as a superglobal variable can improve security and functionality in PHP scripts by storing session data securely on the server-side, rather than exposing sensitive information in URLs or hidden form fields. This helps prevent session hijacking and data tampering by malicious users. Additionally, using $_SESSION allows for easier management of session data across multiple pages and ensures that data is persistent throughout the user's session.

<?php
session_start();

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

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

// Destroy session data
session_unset();
session_destroy();
?>