Is .htaccess the only solution for restricting access to certain parts of a website in PHP?

To restrict access to certain parts of a website in PHP, .htaccess is not the only solution. You can also implement access control within your PHP code by checking user permissions or roles before allowing access to specific pages or content. This can be done by setting up a login system, storing user roles in a database, and validating user permissions on each restricted page.

<?php
session_start();

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

// Check if user has the necessary permission
if($_SESSION['role'] !== 'admin') {
    echo "You do not have permission to access this page.";
    exit();
}

// Restricted content here
?>