What are common pitfalls when using the header() function in PHP?
One common pitfall when using the header() function in PHP is attempting to set headers after content has already been sent to the browser. To avoid this issue, make sure to call header() before any output is sent to the browser, including whitespace. Another pitfall is not using the exit() function after setting a Location header for a redirect, which can lead to unexpected behavior.
<?php
// Correct way to set a header before any output
header('Content-Type: text/html');
// Correct way to set a Location header for a redirect
header('Location: http://www.example.com');
exit();
?>