A redirect that never redirects is one of the most confusing PHP experiences. You write header('Location: dashboard.php'), the page stays put, and PHP answers with:.

The script does not crash. It just silently ignores your header. Here are the five mistakes I see most often, and the one-line fix for each.

A single space or newline before the opening tag is output. PHP sends the headers the moment the first byte leaves the script, so your later header() call arrives too late. (Related: Latest Update: School sends out minibus to catch absent...)

Fix: make 2. Echoing something before the redirect The echo is body output, and body output seals the headers. Process first, display last: do the redirect check at the top, and only echo after you know you are staying on the page. And always put exit; right after a redirect header β€” a redirect does not stop the script by itself. 3. A closing tag with a newline in an included file Your main file can be perfect and still fail, because an included file ends with ?> followed by a newline. That newline is output too. Fix: omit the closing ?> tag entirely in files that contain only PHP. PHP's own style guidance recommends it, and it removes a whole class of invisible accidents. Some editors save files with three invisible bytes (EF BB BF) at the very start. PHP treats them as output, so the warning points at line 1 even though the file looks clean. Fix: re-save the file as "UTF-8 without BOM" (in VS Code: click the encoding in the status bar, then "Save with Encoding"). Check every included file the same way. The warning actually tells you where the output started β€” read the first half, not the second: Line 2 is the bug. Line 9 is only the victim β€” the header() call that arrived too late. Open the file from the first half and look at that exact line. I wrote a full walkthrough of this error β€” every cause, the headers_sent() debugging trick, and when output buffering is (and is not) the right answer β€” on my blog: How to Fix "Headers Already Sent" Error in PHP The rule behind all five mistakes is one line: headers before body. Once your first byte of output happens only after the headers are sent, the warning disappears for good. (Related: Tech Report: University rolls out $5m smart system that...)

The echo is body output, and body output seals the headers. Process first, display last: do the redirect check at the top, and only echo after you know you are staying on the page. And always put exit; right after a redirect header β€” a redirect does not stop the script by itself.

According to recent reports, your main file can be perfect and still fail, because an included file ends with ?> followed by a newline. That newline is output too.

Fix: omit the closing ?> tag entirely in files that contain only PHP. PHP's own style guidance recommends it, and it removes a whole class of invisible accidents.

Some editors save files with three invisible bytes (EF BB BF) at the very start. PHP treats them as output, so the warning points at line 1 even though the file looks clean.

Fix: re-save the file as "UTF-8 without BOM" (in VS Code: click the encoding in the status bar, then "Save with Encoding"). Check every included file the same way.

The warning actually tells you where the output started β€” read the first half, not the second:.

Line 2 is the bug. Line 9 is only the victim β€” the header() call that arrived too late. Open the file from the first half and look at that exact line.

I wrote a full walkthrough of this error β€” every cause, the headers_sent() debugging trick, and when output buffering is (and is not) the right answer β€” on my blog: How to Fix "Headers Already Sent" Error in PHP.

The rule behind all five mistakes is one line: headers before body. Once your first byte of output happens only after the headers are sent, the warning disappears for good.

β†’ Read original article