What are the common pitfalls to watch out for when using header() function for redirection in PHP?

Common pitfalls when using the header() function for redirection in PHP include not using exit() after the header() function, not checking for output buffering, and not validating user input before using it in the header() function.

// Correct way to use header() function for redirection in PHP

// Start output buffering
ob_start();

// Perform validation on user input
$redirect_url = filter_var($_POST['redirect_url'], FILTER_VALIDATE_URL);

if ($redirect_url) {
    // Redirect to the validated URL
    header("Location: $redirect_url");
    exit(); // Ensure no further output is sent
} else {
    // Handle invalid input
    echo "Invalid URL provided";
}

// Flush output buffer and end buffering
ob_end_flush();