How can PHP be used to restrict access to files for users who are not logged in?

To restrict access to files for users who are not logged in, you can use PHP sessions. When a user logs in, you can set a session variable to indicate that they are authenticated. Then, on pages where you want to restrict access, you can check if this session variable is set. If it is not set, you can redirect the user to a login page or display an access denied message.

<?php
session_start();

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