How can multiple pages be protected with a single password input in PHP?

To protect multiple pages with a single password input in PHP, you can create a session variable to store the password once it's entered correctly. Then, on each protected page, check if the session variable is set and matches the correct password. If it does, allow access to the page, otherwise, redirect to a login page.

<?php
session_start();

$correct_password = "password123";

if(isset($_POST['password'])){
    if($_POST['password'] == $correct_password){
        $_SESSION['authenticated'] = true;
    } else {
        echo "Incorrect password";
    }
}

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