No articles found
Try different keywords or browse our categories
[SOLVED]: Headers already sent error in PHP Full Tutorial
Quick guide to fix 'Headers already sent' errors in PHP. Essential fixes with minimal code examples.
The ‘Headers already sent’ error occurs when PHP tries to send HTTP headers after content has already been output. This error prevents proper header manipulation like redirects, cookies, and sessions.
Common Causes and Fixes
1. Output Before Headers
<?php
// ❌ Error: Headers already sent
echo "Some content";
header("Location: /page.php"); // Error!
?>
<?php
// ✅ Fixed: Headers before output
header("Location: /page.php");
exit; // Important!
echo "Some content"; // This won't execute
?>
2. Whitespace Before PHP Opening Tag
// ❌ Error: Hidden whitespace before <?php
[SPACE OR NEWLINE HERE]<?php
session_start(); // Error!
?>
<?php
// ✅ Fixed: No whitespace before opening tag
session_start();
?>
3. Whitespace After PHP Closing Tag
<?php
session_start();
?>
[SPACE OR NEWLINE HERE] // ❌ Error!
<?php
session_start();
// ✅ Fixed: No closing tag or no content after it
?>
4. Output from Included Files
<?php
// file.php has output before this line
include 'file.php';
header("Location: /success.php"); // Error!
?>
<?php
// ✅ Fixed: Clean included files
include 'file.php'; // Ensure file.php has no output
header("Location: /success.php");
exit;
?>
5. Using Output Buffering
<?php
// ❌ Error: Direct output
echo "Content";
header("Location: /page.php"); // Error!
?>
<?php
// ✅ Fixed: Output buffering
ob_start();
echo "Content";
ob_end_clean(); // Discard output
header("Location: /page.php");
exit;
?>
6. Session Start Issues
<!DOCTYPE html>
<html>
<?php
session_start(); // ❌ Error: HTML output before
?>
<?php
session_start(); // ✅ Fixed: Before any output
?>
<!DOCTYPE html>
<html>
7. Cookie Setting After Output
<?php
echo "Content";
setcookie("name", "value"); // ❌ Error!
?>
<?php
setcookie("name", "value"); // ✅ Fixed: Before output
echo "Content";
?>
8. Redirect Function
<?php
function redirect($url) {
echo "Redirecting..."; // ❌ Error: Output before header
header("Location: $url");
exit;
}
?>
<?php
function redirect($url) {
header("Location: $url"); // ✅ Fixed: Header before output
exit;
}
?>
9. Checking for Output
<?php
// ❌ Error: No check
if (headers_sent()) {
die("Headers already sent");
}
header("Location: /page.php");
?>
<?php
// ✅ Fixed: Proper check
if (!headers_sent()) {
header("Location: /page.php");
exit;
} else {
// Handle error
die("Cannot redirect, headers already sent");
}
?>
10. Using Output Buffering Throughout
<?php
// ✅ Best practice: Buffer all output
ob_start();
// All your code here
header("Location: /page.php");
ob_end_flush();
?>
Quick Debugging Steps
- Check error message for file and line number
- Look for whitespace before
<?phpor after?> - Verify no output before header functions
- Use
headers_sent()to check if headers were sent - Check included files for hidden output
Prevention Tips
- Place header functions at the top of your script
- Remove closing
?>tags when file contains only PHP - Use output buffering if needed
- Check
headers_sent()before sending headers - Ensure included files don’t produce output
- Use
exitafter redirects
Remember: HTTP headers must be sent before any content. Always ensure headers are set before any output occurs.
Related Articles
Fix: Cannot modify header information error in PHP - Quick Solutions
Quick guide to fix 'Cannot modify header information' errors in PHP. Essential fixes with minimal code examples.
Fix: Parse error: syntax error, unexpected token error in PHP - Quick Solutions
Quick guide to fix 'Parse error: syntax error, unexpected token' errors in PHP. Essential fixes with minimal code examples.
Fix: PHP file not executing on server error - Quick Solutions
Quick guide to fix 'PHP file not executing on server' errors. Essential fixes with minimal code examples.