PHP Headers Already Sent Error Solution (Complete Fix Guide)
The "headers already sent" error is one of the most common PHP errors when working with redirects, sessions, cookies, and authentication.You may see a...
Send me the error and I'll diagnose it — usually within 2 hours.
The "headers already sent" error is one of the most common PHP errors when working with redirects, sessions, cookies, and authentication.
You may see an error like:
Warning: Cannot modify header information - headers already sent by
or in Laravel:
Cannot modify header information - headers already sent
This happens when PHP tries to send HTTP headers after some output has already been sent to the browser.
In this guide, you will learn why this happens and how to fix it.
Why Does "Headers Already Sent" Error Happen?
PHP headers must be sent before any HTML, text, spaces, or output.
Example:
echo "Hello"; header("Location: /dashboard");
The browser already received:
Hello
so PHP cannot send the redirect header anymore.
That causes:
Cannot modify header information - headers already sent
Fix 1: Remove Extra Spaces Before PHP Opening Tag
A very common cause is whitespace before:
<?php
Wrong:
<?php echo "Hello";
The empty line before PHP is already output.
Correct:
<?php echo "Hello";
Make sure there is nothing before the PHP opening tag.
Fix 2: Remove Closing PHP Tag
In pure PHP files, avoid:
?>
at the end of the file.
Example:
<?php class UserController { } ?>
The closing tag can accidentally add spaces or new lines.
Better:
<?php class UserController { }
Fix 3: Check for Unexpected Output
Search your code for:
echo print var_dump dd() dump()
Example:
dd($user); return redirect('/dashboard');
The dd() output is sent before the redirect.
Remove it:
return redirect('/dashboard');
Fix 4: Laravel Redirect Headers Already Sent
A common Laravel example:
public function store() { echo "debug"; return redirect('/home'); }
The echo sends output before Laravel sends redirect headers.
Fix:
public function store() { return redirect('/home'); }
Fix 5: Check Blade Files
Laravel Blade files can also cause this.
Example:
<!-- resources/views/test.blade.php --> hello @php session_start(); @endphp
The HTML output happens first.
Avoid starting sessions manually in Blade.
Use Laravel:
session(['key'=>'value']);
instead.
Fix 6: Check UTF-8 BOM Characters
Some editors save PHP files with BOM characters.
This invisible character is output before PHP runs.
Fix:
VS Code:
- Open the PHP file
- Click encoding in the bottom bar
- Select:
Save with Encoding
- Choose:
UTF-8
without BOM
Fix 7: Use Output Buffering (Temporary Solution)
PHP provides output buffering:
<?php ob_start(); header("Location: dashboard"); ob_end_flush();
This delays output.
However, this should not hide bad code structure. Find and remove the unwanted output first.
Fix 8: Laravel Session or Cookie Errors
If you see:
session_start(): Cannot send session cookie
check if something runs before Laravel starts.
Examples:
Bad:
echo "test"; session_start();
Good:
session_start(); echo "test";
How to Find the File Causing the Problem
The error usually tells you:
Example:
headers already sent by /home/user/app/test.php:10
Open that file and check line 10.
Look for:
- spaces
- HTML output
- echo statements
- debugging code
- closing PHP tags
Laravel Headers Already Sent After Deployment
If your Laravel app works locally but fails on the server:
Check:
- File encoding
- Cached views
- Extra output in middleware
- Debug statements
- Custom PHP files included before Laravel
Clear Laravel cache:
php artisan optimize:clear
Then:
php artisan optimize
Final Checklist
When you see:
Cannot modify header information - headers already sent
Check:
✓ Remove spaces before <?php
✓ Remove closing PHP tags
✓ Remove echo/print/debug output
✓ Check Blade templates
✓ Check file encoding
✓ Clear Laravel cache
✓ Find the exact file and line from the error
The error is usually caused by a small piece of output being sent earlier than expected.
If you are stuck with a Laravel redirect, login, session, or cookie issue caused by this error, send me the error message, and I can help identify the cause.
Still not fixed?
Share your error log and I'll tell you exactly what's wrong. I've seen this error in every form across 50+ Laravel projects.