How can a variable be passed from a session to display user information in a guestbook?

To pass a variable from a session to display user information in a guestbook, you can store the user information in a session variable when the user logs in or registers. Then, when displaying the guestbook, retrieve the user information from the session variable and display it alongside the guestbook entries.

// Start the session
session_start();

// Set user information in session variable when user logs in or registers
$_SESSION['username'] = 'JohnDoe';
$_SESSION['email'] = 'johndoe@example.com';

// Retrieve user information from session variable and display in guestbook
echo 'Welcome, ' . $_SESSION['username'] . '!'; // Display username
echo 'Email: ' . $_SESSION['email']; // Display email