How can PHP be integrated with login scripts to improve website security and user experience?

Issue: Integrating PHP with login scripts can improve website security by verifying user credentials before granting access to sensitive information. This can also enhance user experience by providing a secure and seamless login process.

<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve user input
    $username = $_POST["username"];
    $password = $_POST["password"];
    
    // Validate user credentials
    if ($username == "admin" && $password == "password123") {
        // Redirect user to dashboard upon successful login
        header("Location: dashboard.php");
        exit;
    } else {
        // Display error message for invalid credentials
        echo "Invalid username or password";
    }
}
?>