How can sessions be used as an alternative to setting global variables in a URI in PHP?

Using sessions can be an alternative to setting global variables in a URI in PHP because sessions allow you to store variables across multiple pages without exposing them in the URL. This can improve security by preventing sensitive information from being visible in the address bar. To implement this, you can set session variables on one page and access them on subsequent pages as needed.

// Start the session
session_start();

// Set session variables
$_SESSION['username'] = 'JohnDoe';
$_SESSION['email'] = 'johndoe@example.com';

// Access session variables on another page
session_start();
echo $_SESSION['username']; // Outputs: JohnDoe
echo $_SESSION['email']; // Outputs: johndoe@example.com