What are some alternative methods for protecting directories in PHP if the hoster restricts certain configurations?

If the hoster restricts certain configurations that prevent you from using traditional methods like .htaccess files to protect directories in PHP, you can still protect directories by using PHP code to restrict access based on user authentication. One way to do this is by creating a login system where users need to enter a username and password to access the directory.

<?php
session_start();

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

// Your protected directory content goes here
echo "Welcome to the protected directory!";
?>