What role do headers play in PHP files and how can they be used to control access to files?

Headers in PHP files are used to send additional information to the browser before any content is displayed. They can be used to control access to files by setting specific headers like "Content-Type", "Content-Disposition", or "Location". By using headers, you can restrict access to certain files by checking for specific conditions before allowing the file to be accessed.

<?php
// Check if the user is logged in before allowing access to a file
session_start();

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

// Proceed with displaying the file content
// Your file display logic here
?>