How do sessions provide a more secure way to carry user data between pages in PHP compared to other methods?
Sessions provide a more secure way to carry user data between pages in PHP compared to other methods because session data is stored on the server, not on the client side. This means that sensitive information, such as user credentials, is not exposed to potential security risks like cross-site scripting attacks. Additionally, session data is encrypted by default, adding an extra layer of protection.
<?php
// Start the session
session_start();
// Store data in the session
$_SESSION['username'] = 'example_user';
$_SESSION['email'] = 'user@example.com';
// Retrieve data from the session on another page
session_start();
$username = $_SESSION['username'];
$email = $_SESSION['email'];
// Destroy the session when no longer needed
session_destroy();
?>