Is there a recommended best practice for handling login failures and success redirects in PHP applications?

When handling login failures in PHP applications, it is recommended to display an error message to the user and redirect them back to the login page. On the other hand, for successful logins, users should be redirected to a dashboard or home page.

// Check login credentials
if($login_successful) {
    // Redirect to dashboard
    header("Location: dashboard.php");
    exit();
} else {
    // Display error message and redirect back to login page
    echo "Invalid username or password";
    header("Location: login.php");
    exit();
}