How can a password prompt be implemented before accessing a specific page in PHP?

To implement a password prompt before accessing a specific page in PHP, you can create a simple form that asks for a password. Upon form submission, you can check if the entered password matches the expected password. If it does, you can allow access to the specific page, otherwise, you can redirect the user back to the login page.

<?php
$expectedPassword = "secretPassword";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $enteredPassword = $_POST["password"];

    if ($enteredPassword == $expectedPassword) {
        // Password is correct, allow access to the specific page
        // Add your page content here
        echo "Welcome to the specific page!";
    } else {
        // Incorrect password, redirect back to the login page
        header("Location: login.php");
        exit();
    }
}
?>

<form method="post" action="">
    <label for="password">Password:</label>
    <input type="password" name="password" id="password">
    <button type="submit">Submit</button>
</form>