How can PHP sessions be used to protect web pages and images, and what are the limitations of this approach?

To protect web pages and images using PHP sessions, you can restrict access to these resources by checking if a session variable is set before serving the content. This way, only authenticated users with an active session can access the protected resources. However, this approach has limitations such as not being able to prevent direct access to the files if the user knows the URL.

<?php
session_start();

if(!isset($_SESSION['logged_in'])) {
    header('Location: login.php');
    exit;
}

// Serve protected content here
?>