How can a beginner in PHP effectively implement a simple login system for a web application that involves user authentication and access control?

To implement a simple login system in PHP for user authentication and access control, you can create a login form where users can input their credentials (username and password). Upon submission, you can validate these credentials against a database of registered users. If the credentials match, you can set a session variable to indicate that the user is logged in and grant access to restricted pages.

<?php
session_start();

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Check if the username and password are set
    if (isset($_POST["username"]) && isset($_POST["password"])) {
        // Validate the username and password (you can use a database for this)
        $username = "admin";
        $password = "password";

        if ($_POST["username"] == $username && $_POST["password"] == $password) {
            // Set session variable to indicate user is logged in
            $_SESSION["loggedin"] = true;
            header("Location: dashboard.php");
            exit();
        } else {
            echo "Invalid username or password";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login</h2>
    <form method="post" action="">
        <label for="username">Username:</label><br>
        <input type="text" id="username" name="username"><br>
        <label for="password">Password:</label><br>
        <input type="password" id="password" name="password"><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>