How can PHP and HTML be effectively integrated to create a user login system for a web project?

To create a user login system for a web project, PHP can be used to handle the backend logic such as user authentication and session management, while HTML can be used to create the user interface for the login form. By integrating PHP and HTML effectively, you can create a seamless user login experience for your web project.

<?php
session_start();

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

    // Perform user authentication here (e.g., check credentials against a database)

    if (/* authentication successful */) {
        $_SESSION['username'] = $username;
        header('Location: dashboard.php');
        exit();
    } else {
        $error = "Invalid username or password";
    }
}
?>

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