How To Fix "Warning: Cannot Modify Header Information - Headers Already Sent" In PHP?

Published October 3, 2024

Problem: PHP Header Modification Error

The "Warning: Cannot Modify Header Information - Headers Already Sent" error in PHP happens when you try to change HTTP headers after content has been sent to the browser. This error often occurs when output appears before header() or setcookie() functions are called, stopping PHP from setting or changing headers as intended.

Identifying the Source of the Problem

Locating unexpected output

To fix the "Headers Already Sent" error, you need to find the source of unexpected output. Here are some areas to check:

  • Check for whitespace before PHP opening tags: Look for spaces, tabs, or newline characters before the

  • Look for echo or print statements: Search your code for any echo, print, or similar output functions that might run before your header-related code. These can send content to the browser too early.

  • Examine included files: Check any files you're including or requiring in your script. These can also contain unexpected output that causes the error.

To locate the problem, start at the top of your main PHP file and work down, looking for these issues. Pay attention to the line number in the error message, as it often points to where the headers were sent, usually just after the problematic output.

Tip: Use output buffering

If you're having trouble pinpointing the source of unexpected output, consider using output buffering. You can start output buffering at the beginning of your script with ob_start(). This will catch any output and allow you to send headers later in your code. Remember to call ob_end_flush() at the end of your script to send the buffered content to the browser.

Solutions to Fix the "Headers Already Sent" Error

Rearranging code structure

To fix the "Headers Already Sent" error, you can rearrange your code:

  • Move header functions to the top: Put all functions that change headers, like header() or setcookie(), at the start of your PHP file. This helps these functions run before any content goes to the browser.

  • Call session_start() early: If you use sessions, call session_start() at the very start of your script, before any output is made.

Tip: Use Include Files Wisely

If your PHP script uses include files, make sure these files don't output any content or whitespace before header functions are called. Place include statements for files containing header-related code at the top of your main script.

Removing early output

Remove any output that might cause the error:

  • Remove spaces before PHP tags: Delete any spaces, tabs, or new lines that come before the

  • Close PHP tags correctly: When closing PHP tags, use ?> with no spaces after. If the file has only PHP code, you can skip the closing tag.

  • Check for echo statements: Look for any echo or print statements that might run before your header functions. Remove or move these statements as needed.

Using output buffering

Output buffering can help prevent the "Headers Already Sent" error:

  • Output buffering keeps the script's output in a buffer instead of sending it to the browser right away. This lets you change headers even if there's output earlier in your script.

  • To use output buffering, add ob_start() at the start of your script. This function begins output buffering.

  • At the end of your script, use ob_end_flush() to send the buffered content to the browser.

Here's a simple example of how to use output buffering:

<?php
ob_start();
// Your PHP code here, including any header changes
ob_end_flush();
?>

By using these solutions, you can fix the "Headers Already Sent" error and make your PHP script work correctly.