How can PHP sessions or cookies be utilized to access restricted data on a website?

To access restricted data on a website using PHP sessions or cookies, you can set a session variable or cookie upon successful authentication. Then, on subsequent requests, you can check for the presence of this session variable or cookie to determine if the user is authenticated and allowed to access the restricted data.

// Start the session
session_start();

// Check if the user is authenticated
if(isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true){
    // Access restricted data here
    echo "Welcome to the restricted area!";
} else {
    // Redirect to login page if not authenticated
    header("Location: login.php");
    exit();
}