How can PHP work in conjunction with JavaScript to control page access within a browser?

To control page access within a browser using PHP and JavaScript, you can use PHP to handle server-side authentication and authorization checks, and then use JavaScript to interact with the PHP scripts and control the page display based on the authentication status.

<?php
session_start();

// Check if user is authenticated
if(!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
    header('Location: login.php');
    exit;
}
?>