How can PHP differentiate between public and protected areas of a website for user interaction?

To differentiate between public and protected areas of a website for user interaction, you can use PHP sessions to track the user's login status. When a user logs in, you can set a session variable to indicate that they are authenticated. Then, in your PHP code, you can check this session variable to determine whether the user has access to protected areas of the website.

<?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 protected areas
    echo "Welcome to the protected area!";
} else {
    // User is not logged in, redirect to login page
    header("Location: login.php");
    exit();
}
?>