What are the alternatives to using .htaccess for restricting access to directories containing PHP scripts, especially in situations where server ownership is limited?

When server ownership is limited and .htaccess files cannot be used to restrict access to directories containing PHP scripts, an alternative approach is to use PHP to check for authentication before allowing access to the files. This can be achieved by placing a PHP script at the beginning of each PHP file or in a common include file that checks for authentication credentials before executing the script.

<?php
// Check authentication before allowing access to the PHP script
$allowed_users = array('user1', 'user2', 'user3');
if (!in_array($_SERVER['PHP_AUTH_USER'], $allowed_users)) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Basic realm="Restricted Area"');
    die('Access Denied');
}
// Continue with the rest of the PHP script