You are here:Home»KB»Web Server»Headers»Cannot modify header information - headers already sent by
Tuesday, 04 January 2011 00:00

Cannot modify header information - headers already sent by

Written by

Solution 1:

This problem is actually listed in the "common problems" section of the installation instructions, but many users only seem to run into it when they start configuring their site after the initial setup. So here we go again:

This is commonly known as the "whitespace problem".

The error message typically looks something like

Warning: Cannot modify header information - headers already sent by (output started at /path/to/geeklog/public_html/config.php:581) in /path/to/geeklog/public_html/system/lib-sessions.php on line 180

(line numbers and file names may vary). The problem is that many editors seem to add additional blanks (spaces) and/or empty lines at the end of a file when you edit it. This so-called whitespace is then sent to the browser when the file is loaded and interferes with the header of a page that Geeklog tries to send to the browser, often causing problems such as login problems.
 



The fix is, obviously, to remove that whitespace from the file. Read the error message carefully. It says "output started at ..." followed by a file name 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.


Since the editor you've been using caused this problem in the first place, you should use another editor to fix the problem and remove the whitespace. See this and this forum thread for some recommendations for editors to use on Windows. Unix/Linux users can't go wrong with vi.

Taken from here: Cannot modify header information - headers already sent by ... - Geeklog


Solution 2:

This error message is commonly seen by programmers starting to use PHP. Understanding why this error occurs will help find the solution.

PHP handles lots of the work of generating web pages for you, without you even having to ask. A web page is composed of two parts, the header and the body.

The header is generally stuff that you don’t need to worry about, is generated automatically, and contains information about the page, the server, related cookies, and so on. The header information is important, but it is not typically seen by the user. Here are some examples:

Date: Mon, 10 Jul 2006 18:51:59 GMT
Server: Apache/2.2.0 (Unix) mod_ssl/2.2.0 OpenSSL/0.9.7g
Content-Encoding: gzip
Content-Type: text/html

Sometimes programmers want to change some of the header values. For example, if the PHP if generating XML output, the Content-Type should be changed to reflect this. Another common example is in redirecting the user’s browser to a different web page using the Location header element as described in this Tech-Recipe.

The header must come first in the response from a web server and is separated from the body by one blank line. The reason this error occurs is that some part of the body of the web page has been sent to the user already when a request is made to set a header value. Because PHP simplifies many things for you, the problem may be hiding in plain site.
 


Here are some guidelines for finding the problem:

 

  1. Find the header() statement that is causing the problem. The error must be at or before this line.
  2. Look for any statements that could send output to the user before this header statement. If you find one or more, find some way to move the header statement before them. Complex conditional statements may complicate the issue, but they may also help solve the problem. Consider a conditional expression at the top of the PHP script that determines the header value as early as possible and sets it there.
  3. Make sure there is no white space outside of the php start and end tags. While a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit.
  4. Make sure you FTP program is set to binary mode. If not this can incorrectly upload text files messing about especially with the carriage returns and possibly could introduce whitespace. If this is the case re-upload all files in the new mode.
  5. If using joomla, empty all caches + purge sh404sef urls and make sure the live site setting is correct.
Read 1329 times Last modified on Thursday, 03 December 2015 19:49