How can JavaScript be utilized to enforce restrictions on accessing a PHP page, and what are the limitations of this approach?

To enforce restrictions on accessing a PHP page using JavaScript, you can send an AJAX request to a PHP script that checks for certain conditions (such as user authentication) before allowing access to the page. This approach can help enhance security by adding an additional layer of validation on the client-side.

<?php
session_start();

if(!isset($_SESSION['user_id'])) {
    http_response_code(403);
    exit;
}

// Your PHP page code here
?>