How can PHP sessions be used to secure a login area and restrict access to certain pages?

To secure a login area and restrict access to certain pages, PHP sessions can be used to store the user's login status. When a user successfully logs in, a session variable can be set to indicate their authenticated status. On restricted pages, you can check for this session variable and redirect users who are not logged in.

<?php
session_start();

// Check if user is not logged in, redirect to login page
if (!isset($_SESSION['logged_in'])) {
    header('Location: login.php');
    exit();
}

// Restricted page content here
?>