How do I fix "headers already sent" in PHP?
HTTP headers must be sent before any body output. Anything echoed — including a single space before <?php or after ?> — sends the headers and locks them.
The error tells you exactly where the output started:
Warning: Cannot modify header information - headers already sent by
(output started at /var/www/config.php:42)Go to that file and line.
#The usual causes
Whitespace after the closing tag. A newline after ?> at the end of an included file is output.
A BOM at the start of the file. Some editors write a UTF-8 byte order mark, which is three invisible bytes of output. Save as "UTF-8 without BOM".
Debug output you forgot. A stray var_dump or echo earlier in the request.
An error message. With display_errors on, a warning printed earlier counts as output.
#The fix that prevents all of them
Omit the closing ?> in any file that is pure PHP:
<?php
declare(strict_types=1);
function helper(): string {
return 'ok';
}
// no closing tag — nothing can follow itThis is standard practice in every serious PHP codebase, and PSR-12 requires it.
#And always exit after a redirect
header('Location: /thanks.php');
exit;header() only queues the redirect. Without exit, the rest of the script still runs — and can still write to your database a second time.