What are the best practices for using the header() function in PHP to avoid header modification errors?
When using the header() function in PHP, it is important to ensure that no output has been sent to the browser before calling the function. This is because headers must be sent before any output is sent to the browser. To avoid header modification errors, make sure to call the header() function before any HTML, text, or whitespace is output.
<?php
ob_start(); // Start output buffering
// Your PHP code here
header("Location: http://www.example.com"); // Redirect header
ob_end_flush(); // Flush output buffer
?>