How can I prevent users from bypassing the login page and accessing specific pages directly in PHP?

To prevent users from bypassing the login page and accessing specific pages directly in PHP, you can use session variables to track the user's authentication status. When a user logs in successfully, set a session variable to indicate that they are authenticated. On each restricted page, check if the session variable is set to ensure that the user is logged in before allowing access.

<?php
session_start();

// Check if user is not logged in and redirect to login page
if (!isset($_SESSION['authenticated'])) {
    header('Location: login.php');
    exit();
}

// Restricted page content here
?>