views:

632

answers:

5

I'm looking for things that might trigger the following PHP warning:

PHP Warning: Cannot modify header information - headers already sent in Unknown on line 0

+1  A: 

I think whats happening is one of the built in php functions is outputting something. I've seen this with a couple of the IMAP functions where they out put just a " " (space character) and it screws things up. You can thry tracking it down using Xdebug or the Zend debugger, but i f you have neither try wrapping output buffering around some of the functions you think may be cause it.

ob_start();
callYourFunction();
ob_end_clean();

Do this one function at a time and when the error goes away you'll know which function is cause you the problem, then you can either file a bug report or just leave it in as a hack. But at least then you know what function is cause the issue.

Edit: The fact that is says your output is occurring on line 0 means that it's a C level function doing the output, not any code that's been written using PHP.

zodeus
A: 

Have you checked your files for unintended UTF-8 BOMs?

Gumbo
+3  A: 

It might be a lot of things, but as the others said, it's often just a space lying around somewhere that gets outputted and then a header() command is sent which normally is fine, but not after starting to send content back (potentially just a space in this case).

Using ob_start() stops the output from going out right away by buffering it. So it's a potential solution, or at least way to diagnose where it's coming from as zodeus said.


One common thing that causes those lose spaces are in this scenario.

global.php

<?php
  $variable = 1;
  $database = 'something else';
?> <-- A space here
 <-- Or here

index.php

<?php

  require('global.php');
  $var = dosomething();
  header('Location: http://www.example.com/');

?>

One way to fix that is to remove the ?> at the end of the global.php file. You don't need those, they are only useful if you start putting HTML for example after your PHP code. So you'd have:

<?php
  $variable = 1;
  $database = 'something else';

And when you do the require(), the space is not outputted before the header().


Just to illustrate the problems with content outputted and headers is that other common case that gives a similar error. It happens when you forget to stop the processing after a redirection using header().

if ($notLoggedIn) {
  header('Location: http://www.example.com/login.php');
}
echo 'Welcome to my website'; // this will be outputted, 
                              // you should have an exit() 
                              // right after the header()
lpfavreau
As always, a comment explaining the downvote is appreciated so I can make adjustments, learn or delete the question if it is wrong. Thanks.
lpfavreau
A: 

The error tells you that something has sent output, which would force headers to be sent, because the headers must be written before the body of the http message.

The most common problem I have found is text in headers. vis:

<?php // myfile.php
  include 'header.php';
?>

and in header.php:

<?php // header.php
   ....
 ?>

What you can't see here is that there is a blank - either a space or CR/LF after the closing '?>'. This is output because the php standard says that anything outside the php tags is output as html.

The solution is to make sure that you make sure to erase everything after the closing '?>'

dar7yl
there's a PHP _standard_ ??!?!
Alnitak
+1  A: 

Turned out that it was the line

ob_start("ob_gzhandler");

that caused the warning. This has been reported and fixed in 2001, it seems, but for some reason it keeps coming back.

kari.patila
Good job finding your solution! You might choose your answer as the correct one then so it can be seen at the top of the other answers. You might also edit it to add some more details so it can help someone else.
lpfavreau
There's a 48 hour time limit on accepting one's own answers. As for the details, I can't think of anything more to add.
kari.patila
Well, I was thinking maybe on more details on "reported and fixed in 2001". By whom? By the PHP community? Do you have a link to a bug report? Or some internal bug by you or your team? Where was it called? At the beginning of all your scripts? In an include? In a known library? It might help. :-)
lpfavreau
Ah, here you go: http://bugs.php.net/bug.php?id=9031. The call was made on the first line of index.php, on every page view and not included.
kari.patila
I have the exact same warning, and also tracked it down to ob_gzhandler. My pages still seem to be working ok. Is it safe to ignore this warning (in this case)?
Liam
Just make sure the warning doesn't flood your server's log files.
kari.patila