faqts : Computers : Programming : Languages : PHP : Common Problems : Errors

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

39 of 61 people (64%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

What does the "cannot add more header information" error mean?
How can I find out if I am sending whitespace before calling header()?

Dec 13th, 2003 07:35
Jon Kriek, Nathan Wallace, Rasmus Lerdorf, Nathan Wallace


This seems to be a common problem amongst new users. For some peculiar 
reason, they think that they can output data to the browser, and then 
send header information. Unfortunately, they are confusing the HTML 
meta-refresh tag with the PHP header() function. 
"cannot modify header information - headers already sent" 
Allow me to elaborate: That error is caused by because you are trying 
to send header information AFTER you have sent output to the browser. 
Usually this is a simple case of reordering an echo statement or 
removing white space before and/or after the PHP start and end tags 
(very common issue), however you can also use output buffering with the 
ob_start() and ob_end_flush() functions which will allow you to place 
headers and content in any order you want to. Note output buffering was 
originally designed to solve HTTP header errors. 
<?php 
    ob_start(); 
    // file content 
    ob_end_flush(); 
?> 
Although the ob_end_flush() function is not needed in MOST cases 
because it is called automatically at the end of script execution by 
PHP itself when output buffering is turned on either in the php.ini or 
by calling ob_start().