HTTP responses include a status line, headers, and body. PHP sends headers with header() before any output—whitespace or echo first causes "headers already sent" errors.
Common headers
Content-Type: application/jsonfor APIsCache-Controlfor caching policyLocationwith 302/303 for redirects after POST
Redirect after POST
header('Location: /thanks.php', true, 303);
exit;
Use exit after redirects so no further output runs. PRG pattern (Post-Redirect-Get) prevents duplicate form submissions on refresh.
Playground note
header() affects real HTTP responses—not terminal output here. Learn the sequence; verify on local server.
Self-check
- Why must headers precede body output?
- What problem does Post-Redirect-Get solve?