How can PHP developers prevent users from accessing restricted areas without proper authentication?

To prevent users from accessing restricted areas without proper authentication, PHP developers can implement a session-based authentication system. This involves storing user credentials in a session variable upon successful login and checking for the presence of this variable before granting access to restricted pages.

session_start();

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