How can PHP scripts differentiate between a new page load and a link click when a browser is closed without logging out?

When a browser is closed without logging out, PHP scripts can differentiate between a new page load and a link click by using session variables. By setting a session variable when a user logs in and checking for its presence on each page load or link click, the script can determine if the user is still logged in or if they have closed the browser without logging out.

session_start();

if(isset($_SESSION['logged_in'])){
    // User is still logged in, continue with normal page functionality
} else {
    // Redirect the user to the login page or perform any other necessary action
    header("Location: login.php");
    exit();
}