What are the limitations of using PHP to protect individual non-PHP files compared to .htaccess?

When using PHP to protect individual non-PHP files, the limitations include the need to add PHP code to each file individually, which can be time-consuming and prone to errors. Additionally, PHP protection may not be as secure as using .htaccess, as PHP files can be vulnerable to exploits if not properly configured.

<?php
// Add this code snippet to the top of each non-PHP file to protect it
session_start();

if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
    header('HTTP/1.1 403 Forbidden');
    exit;
}