How can PHP developers prevent unauthorized access to protected areas of a website using session-based authentication?

To prevent unauthorized access to protected areas of a website using session-based authentication, PHP developers can implement a check to verify if the user is authenticated before allowing access to the protected pages. This can be done by setting a session variable upon successful login and checking for its existence on each protected page.

<?php
session_start();

if(!isset($_SESSION['authenticated'])) {
    header("Location: login.php");
    exit();
}
?>