Are you bogged down by the number of times you see the PHP warning “Cannot modify header information – headers already sent…”? It is not only difficult to resolve these errors but also troublesome and frustrating to debug. Take a look at the primary causes of these errors and how you can fix them quickly.


As we know, a web page is made up of two parts – the page header and the body. When a web developer incorrectly creates or modifies a page header, he may see one of the common PHP errors. The error states “Warning: Cannot modify header information – headers already sent by …” with details of the file and line of code with the error. If the developer is unaware of the cause of this error, he may spend hours to get the issue resolved. Understanding why the error occurs will help you find the solution.


Web Page Headers

When you work on PHP for creating websites, PHP would handle the work of generating web pages for you. The header contains page information and is generally generated automatically without requiring developer intervention. The header information is mostly not seen by the user.

Developers may want to modify parts of the page header. Any incorrect configuration may lead to the “Headers already sent” errors. This error may or may not be the first error message on the page. If it is not the first error, then it may have been caused due to previous errors. Fix the errors before this one and this error message would most likely be resolved.

If the error is the first error on the page then it is likely that the cause is due to some error created by the developer, in the PHP code. Here at Templatetoaster website maker, Let us look at each of the causes and the resolution for each.


Causes and Fixes for Errors in Webpage Headers

  • Page body content sent before the header

The header must, as a rule, be sent first in the response from a web server. It is divided from the body by a single blank line. If some section of the body of the web page is sent already before the request to the header, then this error may occur.

Note that the functions that create or modify the headers must be invoked before any other output is shown. Some of the functions used for modifying the HTTP header are:

  1. header
  2. header_remove
  3. session_start
  4. session_regenerate_id
  5. setcookie
  6. setrawcookie

As a first step, find the header statement that is causing the error. The actual error must be at this line or before this line. Next, try to look for statements that could send output before the header statement. If these are present, you need to change the code and move the header statement before such statements.

  • Unparsed HTML before the Header

If there are unparsed HTML sections in a PHP file then these are considered as direct output to the browser. Scripts that trigger a header () call must be called before any raw <html> blocks. You also cannot have any HTML tags present before the header function.

Incorrect usage examples:

  1. <!DOCTYPE html>
  2. <?php <html> header('Location:'); ?>

To fix this error you should separate processing code from output generation code. Place the form processing code right at the beginning of the PHP script.

  • Extra spaces or lines before <?php or after a closing?> php tag

This error also occurs due to whitespace at the beginning or at the end of a PHP file. Extra whitespace may be added by a bad unpacking program or a non-compliant editor like the Notepad, WordPad or TextEdit.

The fix is to remove that whitespace from the file. It says “output started at … “followed by a filename and a line number. That is the file (and line) that you need to edit. Ignore the second file name – that is only a file that included the file that has the whitespace. The first file is the one you have to edit, not the second one.

  • Incorrect PHP Encoding Format

In the last case, we consider the scenario when your code is correct with no white space, HTML tags, and incorrect function calls. However, the PHP code still gives the same error.

This situation is more likely due to how the PHP file was saved. With a text editor like Notepad, you can save PHP file in different encodings like ANSI, Unicode, Unicode big endian, or UTF-8 encoding. If you choose an incorrect encoding format then the PHP script can trigger this error. The best encoding format to save PHP files is the ANSI encoding.

This encoding will not add any hidden whitespaces or characters to the file. Any of the other encodings can actually add extra characters to the PHP file. This can lead to the “headers already sent” error.


Output Buffering as a Workaround

As we have seen above, it is critical to have your code structured properly and ensure that the output is separated from the code. If this cannot be achieved then as a workaround you can try using PHPs Output Buffering.

By default the output buffering is off, the HTML is sent to the browser in pieces as PHP processes the script. If the Output Buffering is on, the HTML is stored in a variable. It is then sent to the browser as one whole at the end of the script.

You can use any of the two methods below to enable output buffering.

  1. Use the Output Buffering setting to enable output buffering. You can configure it in the php.ini file, in the .htaccess file or the .user.ini files.
  2. Use a call to the function ob_start() at the start of the invocation script. This is less reliable for following reasons:
  • A whitespace or a BOM might get added before the function making it ineffective.
  • When attempting to send binary content like a generated image, the buffered unnecessary output causes a problem. This may need an ob_clean() as a further workaround.
  • The buffer which is limited in size can be easily overrun if set to default values.


How TemplateToaster helps?

If you are a newbie or a beginner in developing websites, we recommend that you try using TemplateToaster. This WordPress website Builder lets you create websites for multiple Content Management Systems like WordPress, Joomla, Drupal etc. with the flexibility to choose from a range of templates. You would not need to get into the details of the PHP, CSS, and HTML coding which would prevent getting into errors such as the “headers already sent” error.