Skip to content
PHP Logo

How can PHP sessions be effectively utilized to store user information and control access to certain features like a guestbook?

To store user information and control access to features like a guestbook using PHP sessions, you can set session variables upon login and check these variables to determine if the user has access to certain features. For example, you can set a session variable like $_SESSION['logged_in'] upon successful login and then check this variable before allowing access to the guestbook.

php
<?php
session_start();

// Check if user is logged in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
    // User is logged in, allow access to guestbook
    echo "Welcome to the guestbook!";
} else {
    // User is not logged in, redirect to login page
    header("Location: login.php");
    exit();
}
?>