Solve PHP error: Cannot modify header information - headers already sent
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:
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.
Also see ...
H3Working with server side files with PHP adds a great deal of flexibility to some applications. A common scenario when working with files involves creating a temporary file through uploading or other means and then renaming this file to a permanent location. This recipe describes changing the n
H3To sort an associative array by index values (or keys), use ksort./H3P br / br /div class="code"ksort&40;$capitals&41;;P br /An optional parameter (sort_flags) can be passed to ksort to alter the way that it sorts the array. The following code will sort an associat
H3Web applications overcome the lack of session support in the HTTP protocol primarily through the use of cookies. Transparent to the user and simple for the web developer, cookies provide the foundation of keeping up with who a web user is, where they've been, and what they had for lunch. Well,
H3Once your web application has set a cookie in a user's browser, it is a simple matter to retrieve that value in subsequent page requests. This recipe describes reading a value from a cookie in PHP./H3PAfter a cookie with a name of 'user' has been set in a previous page, the value can be acc
H3An associative array stores an arbitrary index in addition to the values. This is useful for storing configuration information such as in the $_SERVER array./H3PAn individual element of this array can be retrieved with: br / br /div class="code"$_SERVER&91;'SITE_HTMLROOT'&93;P
