What is the role of sessions in PHP for user authentication and data transfer between pages?
Sessions in PHP play a crucial role in user authentication and data transfer between pages. By storing user information in session variables, you can keep track of a user's authentication status throughout their browsing session. Additionally, sessions allow you to pass data between pages without the need to rely on cookies or hidden form fields.
// Start the session
session_start();
// Set a session variable for user authentication
$_SESSION['authenticated'] = true;
// Transfer data between pages using session variables
$_SESSION['username'] = 'JohnDoe';
// Retrieve session data on another page
session_start();
echo $_SESSION['username'];