I am getting the error "Cannot modify header information - headers already sent" from PHP code.
Note that I am NOT getting the error "Cannot modify header information - headers already sent by (output started at [filename]:[linenumber])".
I get the former with SAPI=apache2module and the later with SAPI=cli.
I am quite familiar with the error, and can usually tell exactly where it is coming from. In my current case, I can't. (My best guess so far is that the mail() function is doing output.)
How do I convince PHP to use the form with "by (output started at %s:%d)"?
Examining PHP source, it apparently only uses the later form if the location was recorded. But what makes it record or not?
<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try {
header('Content-Type: text/plain');
printf("before sapi = %s\n", php_sapi_name());
flush();
header('X-test: something');
print "after\n";
} catch (Exception $ex) {
print($ex);
}
Expected and correct, CLI:
$ php flush_test5.php
before sapi = cli
exception 'ErrorException' with message 'Cannot modify header information - headers already sent by (output started at /dir/file.php:10)' in /dir/file.php:12
Stack trace:
#0 [internal function]: exception_error_handler(2, 'Cannot modify h...', '/name/david/bar...', 12, Array)
#1 /dir/file.php(12): header('X-test: somethi...')
$
Obtained, apache2module:
before sapi = apache2handler
exception 'ErrorException' with message 'Cannot modify header information - headers already sent' in /dir/file.php:12
Stack trace:
#0 [internal function]: exception_error_handler(2, 'Cannot modify h...', '/name/david/bar...', 12, Array)
#1 /dir/file.php(12): header('X-test: somethi...')
#2 {main}
I would like to get, from apache2module:
before sapi = apache2handler
exception 'ErrorException' with message 'Cannot modify header information - headers already sent by (output started at /dir/file.php:10)' in /dir/file.php:12
Stack trace:
#0 [internal function]: exception_error_handler(2, 'Cannot modify h...', '/name/david/bar...', 12, Array)
#1 /dir/file.php(12): header('X-test: somethi...')
#2 {main}