How can PHP be used to check for a session variable in specific directory files for authentication?

To check for a session variable in specific directory files for authentication, you can use PHP to start a session, check if the session variable exists, and redirect the user if the variable is not set. This can help ensure that only authenticated users can access certain files within a directory.

<?php
session_start();

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