Menu

5 PHP Header Mistakes That Break Your Redirects (and the One-Line Fix for Each)
πŸ“°

5 PHP Header Mistakes That Break Your Redirects (and the One-Line Fix for Each)

DEV Community: phpΒ·Raja HunainΒ·1 day ago
#3sySpzz8
#devto#dev#line#header#output
Long article (530 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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:

Warning: Cannot modify header information - headers already sent

Enter fullscreen mode

Exit fullscreen mode

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.

1. A blank line before <?php

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.

Fix: make <?php the very first characters of the file. Nothing before it β€” not even a blank line.

2. Echoing something before the redirect

echo 'Welcome back' ; header ( 'Location: dashboard.php' ) ; // Warning

Enter fullscreen mode

Exit fullscreen mode

The echo is body output, and body output seals the headers.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.