How can PHP be used to verify the login status of a user on every page of a website?

To verify the login status of a user on every page of a website, you can create a PHP script that checks if the user is logged in by verifying the presence of a session variable that is set upon successful login. This script can be included at the top of every page to ensure that only authenticated users can access the content.

<?php
session_start();

if(!isset($_SESSION['user_id'])) {
    // Redirect to login page or display an error message
    header("Location: login.php");
    exit();
}
?>