PHP- “headers already sent” error depending on output length?

旧时模样 提交于 2019-12-11 05:38:00

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/17643837/php-headers-already-sent-error-depending-on-output-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!