tags:

views:

43

answers:

4

Getting the following error:

"Warning: Cannot modify header information - headers already sent by (output started at..."

for the following line:

echo '<center>Current Time is '. gmdate("H:i A") . ' GMT (Greenwich Mean Time or UTC)<br />';

If I comment it out it just throws up the error at the next echo statement. Thoughts on why PHP hates my echo statements so much?

Here is my include toward the bottom of the HTML:

<div id="saveCanForm" width="100%">
<?php include('savereport.php'); ?>
</div>
A: 

Because our echos are coming before you are sending the header which is not allowed. Ensure that header go before any of your output.

If you don't want to rearrange you can also use output buffering.

codaddict
works fine on my staging server but breaks on live server. How do you propose I fix it @codaddict?
+5  A: 

It's not the echo statements that are the problem. It looks like you have a header call somewhere later in the file, but you can't send headers once you output any text at all. You could either move the headers to the beginning of the script or alternatively use output buffering.

casablanca
I have an INCLUDE toward the bottom that appears to be the offending party. Worked fine on my staging server which is strange. Any ideas on how to make that include work (I posted the code in my orig question)?
The included file quite likely has a call to `header`, which is causing problems because it occurs after `echo` statements in your main file. The simplest way out is to use output buffering: call [`ob_start()`](http://php.net/manual/en/function.ob-start.php) at the very beginning of your main script and call `ob_end_flush()` at the very end.
casablanca
ob_start and ob_end_flush worked perfectly. You mentioned this as the "simplest" way - out of curiosity is there a "better" way?
No, not without modifying the include file itself.
casablanca
A: 

Headers are dealt with before there is any other output, so if you write something out then PHP can't properly send headers afterwards. At some point in your code you are giving HEAD instructions which hence fails. (There's technical reasons for this, like redirects and so forth)

AlexanderJohannesen
A: 

Its not the echo which is the problem. It is most probably caused by a file that you have included in the .php file. Have you included a file at all? This will probably be at the top. If you included file statement is not at the top of the file make sure it is.

hart1994