What are the common pitfalls when using the header function for page redirection in PHP?

One common pitfall when using the header function for page redirection in PHP is that it must be called before any output is sent to the browser. This means that there should be no HTML, whitespace, or any other content before the header function is called. To solve this issue, you can use output buffering to capture all output before sending it to the browser.

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

// Your PHP code here

ob_end_clean(); // Clear the output buffer without sending any output

header("Location: newpage.php"); // Redirect to new page
exit; // Make sure to exit after the header function to prevent further execution
?>