What are the potential pitfalls of using the Header function to redirect users after login?

Using the Header function to redirect users after login can lead to potential pitfalls such as headers already being sent by the time the function is called, causing the redirection to fail. To solve this issue, it is recommended to use output buffering to prevent any output before calling the Header function.

<?php
ob_start(); // Start output buffering

// Your login logic here

if ($login_successful) {
    ob_end_clean(); // Clean the output buffer
    header("Location: dashboard.php"); // Redirect to dashboard
    exit();
} else {
    ob_end_flush(); // Flush the output buffer
    // Display login error message or form
}
?>