search
php star Featured

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.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 3 min read
PHP Headers Output Buffering HTTP Debugging Code Fixes

The ‘Cannot modify header information’ error occurs when PHP tries to send 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: Cannot modify header information
echo "Content";
header("Location: /page.php");  // Error!
?>
<?php
// ✅ Fixed: Headers before output
header("Location: /page.php");
exit;
echo "Content";  // Won't execute
?>

2. Whitespace Before PHP Tag

// ❌ Error: Hidden output before PHP
[SPACE OR NEWLINE HERE]<?php
session_start();  // Error!
?>
<?php
// ✅ Fixed: No whitespace before PHP
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
?>

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 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 After Output

<!DOCTYPE html>
<html>
<?php
session_start();  // ❌ Error: HTML before session
?>
<?php
session_start();  // ✅ Fixed: Before any output
?>
<!DOCTYPE html>
<html>
<?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: Before output
    exit;
}
?>

9. Checking Headers Status

<?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 {
    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

  1. Check error message for file and line number
  2. Look for whitespace before <?php or after ?>
  3. Verify no output before header functions
  4. Use headers_sent() to check if headers were sent
  5. 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 exit after redirects

Remember: HTTP headers must be sent before any content. Always ensure headers are set before any output occurs.

Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

php

[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.

January 8, 2026
php

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.

January 8, 2026
php

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.

January 8, 2026