PHP 缓冲区为什么\r\n
我在评论中对以下脚本有一些概念性问题(我认为都是相关的)。该脚本运行良好。
<?PHP
ob_start();
// Create string to overflow browser buffer ...?
$buffer = str_repeat(" ", 4096);
// Indicate new header / html content ...?
$buffer .= "\r\n<span></span>\r\n";
for ($i=0; $i<5; $i++) {
echo $buffer.$i;
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
?>
首先,为什么我需要将 \r\n
发送到浏览器?我认为这与标题有关。
其次,为什么中间需要一些HTML?
第三,有很多例子使用256字节而不是4096。但是,如果我使用256,脚本就不起作用。这些例子是否已经过时,这个数字将来会再次改变吗?
//编辑有关源链接
此代码主要来自 php.net 中的 注释sleep()
函数 和解决方案对于这个问题。两者都没有提到为什么要包含 \r\n
。
//编辑有关标头
如果我不添加 \r\n
、HTML 标记和第二组 \r\n
,脚本将无法正确执行在 Chrome 或 Safari 中(它只是一次转储所有值)。
此外,如果在 session_start()
之前调用此函数,则会抛出错误:“无法发送会话缓存限制器 - 标头已发送”。
I have a few conceptual questions (all related, I think) regarding the following script, at the comments. The script works fine.
<?PHP
ob_start();
// Create string to overflow browser buffer ...?
$buffer = str_repeat(" ", 4096);
// Indicate new header / html content ...?
$buffer .= "\r\n<span></span>\r\n";
for ($i=0; $i<5; $i++) {
echo $buffer.$i;
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
?>
First, why do I need to send the \r\n<tag>\r\n
to the browser? I assume it has something to do with headers.
Second, why do I need some HTML in the middle?
Third, there are many examples that use 256 bytes instead of 4096. However, the script doesn't work if I use 256. Are these examples outdated, and will this number change again in the future?
//EDIT REGARDING SOURCE LINKS
This code was gathered mainly from the commentary in php.net sleep()
function and the solution to this SO question. Neither mentions why to include \r\n
.
//EDIT REGARDING HEADERS
If I don't add \r\n
, an HTML tag, and a second set of \r\n
, the script will not execute properly in Chrome or Safari (it just dumps all the values at once).
Additionally, if this is called before a session_start()
, it throws an error: "Cannot send session cache limiter - headers already sent".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,浏览器必须等到获取整个响应才能呈现(只需考虑直到最后一个字符为止都有效的 XML)。但由于这会带来糟糕的用户体验,因此大多数浏览器都会尽早开始解析和渲染内容。
在这里,这个 HTML 片段可能是浏览器实际构建 DOM 并开始渲染的发起者。
正如手册暗示 Web 服务器中可能会包含一些进一步的缓冲,这可能是试图溢出那些缓冲区,它们也被刷新以达到预期的效果。
Normally browser have to wait until they have fetched the whole response until it can be rendered (just think of XML that can be valid until the last character). But since that would make a bad user experience, most browsers start to parse and render the contents as early as possible.
And here this HTML fragment could be the initiator for the browser to actually build the DOM and start rendering.
As the manual hints that there might be some further buffering incorporated in the web server, this might be the attempt to overflow those buffers that they are also flushed in order to have the expected effect.
使用
\r\n
的原因是使输出在使用 Windows 源代码查看器(如notepad.exe
)查看时能够很好地呈现。这里与标题无关。
鉴于代码使用了输出缓冲函数,我不知道为什么他们觉得需要尝试溢出 4kb 缓冲区(标准
php.ini
中的默认值,尽管更多专业人士会选择不使用默认值)输出缓冲)。The reason for using
\r\n
would be make the output render nicely when viewed using a Windows source viewer likenotepad.exe
.Nothing to do with headers here.
Seeing as the code uses the output buffering functions, I have no idea why they feel the need to try and overflow a 4kb buffer (the default in a standard
php.ini
though more professionals would opt for no default output buffering).