Are there any alternative methods to .htaccess for securing PHP files and folders?

Using .htaccess files to secure PHP files and folders is a common practice, but there are alternative methods available. One alternative is to use PHP code within the files themselves to restrict access based on certain conditions, such as user authentication or IP address filtering.

<?php
// Example of restricting access to a PHP file based on user authentication
session_start();

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

// Your PHP code goes here
?>