What are the potential pitfalls of using .htaccess to protect directories in PHP?

Potential pitfalls of using .htaccess to protect directories in PHP include the fact that it relies on server configuration, which may not be available on all hosting environments. Additionally, it may not provide fine-grained control over access permissions, and it can be difficult to manage and maintain as the number of protected directories grows. To solve this issue, a more robust and flexible approach is to use PHP to handle directory protection. This allows for more control over access permissions and can be easily integrated into the application logic.

<?php
session_start();

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