How can PHP beginners handle user authentication and login verification in a newssystem?

To handle user authentication and login verification in a newssystem, PHP beginners can create a login form where users can input their credentials. Upon form submission, the PHP script will check if the username and password match what is stored in the database. If the credentials are correct, the user will be redirected to the news system dashboard; otherwise, an error message will be displayed.

<?php
session_start();

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

        if ($_POST['username'] == $username && $_POST['password'] == $password) {
            $_SESSION['username'] = $username;
            header("Location: news_dashboard.php");
            exit();
        } else {
            $error = "Invalid username or password";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login</h2>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="text" name="username" placeholder="Username" required><br><br>
        <input type="password" name="password" placeholder="Password" required><br><br>
        <button type="submit">Login</button>
    </form>
    <?php if(isset($error)) { echo $error; } ?>
</body>
</html>