What are the potential pitfalls of using PHP header() function for page redirection?

Potential pitfalls of using PHP header() function for page redirection include the need to ensure that no output is sent to the browser before calling the function, as headers must be sent before any content. 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

ob_end_clean(); // Clean the output buffer without sending it to the browser

header("Location: newpage.php"); // Redirect to new page
exit(); // Make sure to exit after redirection
?>