简体   繁体   English

PHP-“标头已发送”错误取决于输出长度?

[英]PHP- "headers already sent" error depending on output length?

I have a script which outputs mysql cell data.我有一个输出 mysql 单元格数据的脚本。 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.我在 SO 上找到的答案没有提到这种输出长度依赖性。

 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 PHP 警告:无法修改标题信息 - 标题已由(输出开始于 /home/mantas/htdocs/asm/article.php:46)在 /home/mantas/htdocs/asm/include/comments_class.php 中第 56 行发送

PHP will buffer output if you want it to. 如果需要,PHP将缓冲输出。 You can control this programmatically with ob_start() , etc. However, there is a further option to set output buffering on in php.ini. 您可以使用ob_start()等以编程方式进行控制。但是,还有一个选项可以在php.ini中设置输出缓冲。

Setting output_buffering=on enables it, while setting output_buffering=4096 will set a limit to the buffer size. 设置output_buffering=on启用它,而将output_buffering=4096设置将限制缓冲区大小。 phpinfo() should tell you if this is enabled, and what the buffer size is. phpinfo()应该告诉您是否启用了它,以及缓冲区的大小是多少。

The PHP reference is here PHP参考在这里

The "headers already send" warning means, you modify the http headers somewhere in your code after you send output to the Client(ie with echo , whitespaces, etc..). “标头已发送”警告意味着,在将输出发送到客户端之后,即在代码中的某个位置修改了HTTP标头(即带有echo ,空格等)。

This warning itself has nothing to do with the content length. 此警告本身与内容长度无关

There are more methods, wich modify the headers: 还有更多方法可以修改标头:

  • header / header_remove 标头/ header_remove
  • session_start / session_regenerate_id session_start / session_regenerate_id
  • setcookie / setrawcookie setcookie / setrawcookie

I also encountered "headers already sent by ..." problems using PHP's xmlWriter.我在使用 PHP 的 xmlWriter 时也遇到了“headers already sent by ...”的问题。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM