How can a PHP beginner protect an online folder with a password prompt?

To protect an online folder with a password prompt in PHP, you can create a simple authentication system using PHP. This involves creating a login form where users must enter a username and password to access the folder. Upon successful login, users are granted access to the protected folder.

<?php
session_start();

$valid_username = 'admin';
$valid_password = 'password123';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($username == $valid_username && $password == $valid_password) {
        $_SESSION['authenticated'] = true;
    } else {
        echo 'Invalid username or password';
    }
}

if (!isset($_SESSION['authenticated']) || !$_SESSION['authenticated']) {
    ?>
    <form method="post" action="">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username"><br>
        <label for="password">Password:</label>
        <input type="password" name="password" id="password"><br>
        <input type="submit" value="Login">
    </form>
    <?php
} else {
    // Code to display protected folder content goes here
}
?>