Is using sessions a better alternative to passing data through URL parameters in PHP?

Using sessions is generally considered a better alternative to passing data through URL parameters in PHP because it is more secure and prevents users from tampering with the data. Sessions store data on the server-side, making it inaccessible to users. This also helps in maintaining cleaner URLs without exposing sensitive information.

<?php
session_start();

// Set data in session
$_SESSION['username'] = 'john_doe';

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

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