8
votes

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

5

5 Answers

8
votes

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.

3
votes

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()
2
votes

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.

1
votes

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

0
votes

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 '?>'