What are the potential pitfalls of using the header function in PHP for redirection?

One potential pitfall of using the header function in PHP for redirection is that it must be called before any output is sent to the browser. If there is any output, such as HTML content or whitespace, before the header function is called, it will result in a "headers already sent" error. To solve this issue, you can use output buffering to capture any output before sending headers.

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

// Your PHP code here

if(condition){
    header("Location: new_page.php");
    exit(); // Make sure to call exit after header redirection
}

ob_end_flush(); // Flush the output buffer
?>