0

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}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
David G.
  • 595
  • 1
  • 4
  • 11
  • if you want to suppress those errors use ob_start(); at the begining of the page and ob_end_flush() at the bottom of the page – Nancy Moore Oct 02 '19 at 17:25
  • I do NOT want to suppress it. I want to fix it. So first, I must find it. (OK, I do a redirect at the end, so the erroneous output would be lost, but if I am getting an error, I want to know about it.) – David G. Oct 02 '19 at 17:33
  • @funk-forty-niner This was not a duplicate because it was asking how to get PHP to give me the information everyone else seems to start with. Where the output started. – David G. Oct 02 '19 at 18:00
  • I've reopened David. Sorry, I made a mistake, they do happen :) – Funk Forty Niner Oct 02 '19 at 18:18
  • Actually, I fully expected it to be mis-marked at least once. Now if I can just get an answer. I would love to find my problem, especially since my real code works reliably for me and not at all for one of my users. – David G. Oct 02 '19 at 18:23

0 Answers0