How can PHP be used to prevent unauthorized access to directories containing sensitive files?

To prevent unauthorized access to directories containing sensitive files, you can use PHP to check if the user is authenticated before allowing access. This can be achieved by implementing a login system where users need to enter valid credentials to access the files.

<?php
session_start();

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

// Otherwise, allow access to sensitive files
// Your code to display or process sensitive files goes here
?>