In the context of PHP, what are the advantages of using sessions over passing data through URLs?
Using sessions in PHP is advantageous over passing data through URLs because it is more secure and prevents sensitive information from being exposed in the URL. Sessions also allow for more flexibility in storing and accessing data across multiple pages without the need to continuously pass parameters in the URL.
// Start a session
session_start();
// Store data in the session
$_SESSION['username'] = 'JohnDoe';
// Retrieve data from the session
$username = $_SESSION['username'];
// Destroy the session when no longer needed
session_destroy();