1

I have this code:

include('file1.php');
include('file2.php');

include('lib1.php');
include('lib2.php');
include('lib3.php');

function __autoload($c) { include('classes/'.$c.'.php'; }

$obj = new Class1();
$obj->method();
...

In $obj->method, there's some functionality to add cookies. I got headers already sent error. So I added var_dump(headers_sent()); and I see a very strange behavior. Here's what I did:

// ... the same code above
var_dump(headers_sent());  // False
$obj = new Class1();       // True, the first line of __cunstruct() inside
$obj->method();            // True, the first line of method()
var_dump(headers_sent());  // True
...

This is strange; how just between the single line headers are sent? How is this possible?

arxoft
  • 1,385
  • 3
  • 17
  • 34
  • 4
    The `var_dump()` itself is output, which would block any additional headers from being sent. – Brobin Aug 16 '14 at 05:09
  • 1
    You could put everything (before the point where the problem occurs) in comments. Then uncomment one by one to see which include causes the problem. Then continue the investigation inside the included file. – Melsi Aug 16 '14 at 05:12

2 Answers2

0

I've had the same issue.
Probabily:

include('file1.php');
include('file2.php');

include('lib1.php');
include('lib2.php');
include('lib3.php');

Are showing some character. Try to open this files with UTF-8 (wihout BOM), Notepad ++ can do this.
Remember to close the php tags or just it open like this:

<?php
$foo = "bar";

Or

<?php 
$foo = "bar";
?>

Remember that, if you forget something like this:

<?php
$foo = "bar";
?>
(blank space)

Will output the blank space and send headers.
Thanks.

Herlon Aguiar
  • 684
  • 2
  • 9
  • 18
0

This problem is most often caused by accidental white space in a php file. I would guess that one of the files that you include has extra white space, which would send headers.

Here is a much better explanation of how headers work.

Community
  • 1
  • 1
Brobin
  • 3,241
  • 2
  • 19
  • 35