1

I have a script which outputs mysql cell data. The "content" cell contains text output, which is of varied length.

When the contents of the "content" cell are small (lets say, a few lines of text), everything works fine. However, when the output reaches several paragraphs or more, I get the 'headers already sent' error.

Does it depend on the output length? Where can I read more about it? The answers I've found on SO mention nothing of such output length-dependency.

 44:   echo "
 45:       <p>".$article['content']."</p>
 46:   ";

If the size of the 'content' output is large, the script produces the following error:

PHP Warning: Cannot modify header information - headers already sent by (output started at /home/mantas/htdocs/asm/article.php:46) in /home/mantas/htdocs/asm/include/comments_class.php on line 56

Onion
  • 1,714
  • 1
  • 23
  • 42

3 Answers3

2

PHP will buffer output if you want it to. You can control this programmatically with ob_start(), etc. However, there is a further option to set output buffering on in php.ini.

Setting output_buffering=on enables it, while setting output_buffering=4096 will set a limit to the buffer size. phpinfo() should tell you if this is enabled, and what the buffer size is.

The PHP reference is here

  • This is strange. It all depends on the output length... wrapping some output (not all of it, just this portion with the output from the "content" cell) solved the issue. That's odd. – Onion Jul 14 '13 at 21:37
0

The "headers already send" warning means, you modify the http headers somewhere in your code after you send output to the Client(i.e. with echo, whitespaces, etc..).

This warning itself has nothing to do with the content length.

There are more methods, wich modify the headers:

  • header / header_remove
  • session_start / session_regenerate_id
  • setcookie / setrawcookie
Philipp
  • 15,377
  • 4
  • 35
  • 52
  • Not only when you `echo` something, but even if there is a plain static html tag in it. btw: I didn't downvote – samayo Jul 14 '13 at 21:24
  • It DOES, because the script WORKS if the contents of the "content" cell are small (for example, just a few lines of text). – Onion Jul 14 '13 at 21:25
  • But thats a side effect of something else - the code you posted isn't really relevant, because the real error appears somewhere before.. – Philipp Jul 14 '13 at 21:26
  • No, it doesn't. Apparently, it has something to do with output buffering as Mike pointed out. – Onion Jul 14 '13 at 21:29
  • Are you using output buffering? I suggest you better look for some withespaces before or after - this is a common error source – Philipp Jul 14 '13 at 21:32
  • Nah, that wasn't the problem... no whitespaces, no BOM. Just had to use output buffering... But the question prevails, since it's still unknown why it depends on the output length. – Onion Jul 14 '13 at 21:40
  • 1
    Your problem is still, you modify the headers after you sent some Content.. to answer your question - after you send 4096 bytes, the buffers are flushed and send to the client - this happens in your posted code and somewhere later you use one of the posted methods above and try to modify the header – Philipp Jul 14 '13 at 21:44
0

I also encountered "headers already sent by ..." problems using PHP's xmlWriter.

I had inserted the "header" instructions for managing the page output type at the end of the script:

I solved it by moving the instructions to the beginning of the script, this order used:

// init header earlier on script
header('Content-Type: text/xml; charset=utf-8');
header('Cache-Control: max-age=0');
header('Content-Disposition: inline');

// new xmlwriter object
$xml = new XMLWriter();

// set direct output stream
$xml->openURI('php://output');

// some code here for creating the xml output
// ....
// ....

// render xml to output
$xml->flush();
Lorenzo Magon
  • 544
  • 5
  • 17