What are the potential pitfalls of using header() to redirect after form submission in PHP?

Using header() to redirect after form submission in PHP can cause issues if there is any output sent to the browser before the header function is called. This can result in a "headers already sent" error. To prevent this, you can use output buffering to capture any output before sending headers.

<?php
ob_start();

// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Handle form data

    // Redirect after form submission
    header("Location: success.php");
    exit();
}

ob_end_flush();
?>