PHP缓冲区为什么\r\\\<br/> [英] PHP buffer why \r\n

查看:97
本文介绍了PHP缓冲区为什么\r\\\<br/>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在评论中,我有一些关于下面的脚本的概念性问题(所有相关的,我认为)。该脚本正常工作。

 <?PHP 
ob_start();

//创建字符串以溢出浏览器缓冲区...?
$ buffer = str_repeat(,4096);

//指示新的标题/ html内容...?
$ buffer。=\r\\\
< span>< / span> \r\\\
; ($ i = 0; $ i <5; $ i ++){
echo $ buffer。$ i;

$ b;
ob_flush();
flush();
sleep(1);
}

ob_end_flush();
?>

首先,为什么我需要发送 \r\\\
< ;标记> \r\\\
到浏览器?我认为它与标题有关。



第二,为什么我需要在中间使用一些HTML?



<第三,有很多使用256字节而不是4096的例子。但是,如果我使用256,脚本不起作用。这些例子是否过时了,这个数字将来会再次发生变化吗?



//编辑源代码链接



这段代码主要是从 php.net sleep()函数这个SO问题的解决方案。也没有提及为什么要包含 \r\\\



//编辑标题标题



如果我不添加 \r\\\
,一个HTML标签和第二组 \r\\\
,该脚本将无法在Chrome或Safari中正确执行(它只是立即转储所有值)。



另外,如果在 session_start()之前调用它,则会引发错误:无法发送会话缓存限制器 - 标题已经发送。

解决方案


首先,为什么我需要发送 \r \\\
< tag> \r\\\
到浏览器?我认为它与标题有关。



其次,为什么我需要中间的HTML?


通常,浏览器必须等到获取完整的响应后才能呈现(只需考虑可以在最后一个字符之前有效的XML)。但是,由于这会造成糟糕的用户体验,因此大多数浏览器都会尽早解析并呈现内容。



此HTML片段可能是浏览器实际构建DOM并开始渲染。


第三,有很多使用256字节而不是4096的例子。但是,脚本如果我使用256,那么这个例子不起作用。这些例子是否过时,并且这个数字会在未来再次发生变化?

暗示可能会在Web服务器中引入一些进一步的缓冲区,这可能是为了达到预期效果而将这些缓冲区溢出的尝试。


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".

解决方案

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?

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.

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?

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.

这篇关于PHP缓冲区为什么\r\\\<br/>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆