PHP:在sleep()之前和之后输出数据?

发布于 2024-09-18 10:07:38 字数 176 浏览 3 评论 0原文

这纯粹是为了了解有关输出缓冲的更多信息,仅此而已。我想做的是将字符串回显到浏览器,休眠 10 秒,然后回显其他内容。通常浏览器会等待整整 10 秒,然后发布整个结果,我该如何阻止呢?一个例子:

ob_start();
echo "one";
sleep(10);
echo "two";

This is purely for learning more about output buffering and nothing more. What I wish to do is echo a string to the browser, sleep 10 seconds, and then echo something else. Normally the browser would wait the full 10 seconds and then post the whole result, how I would I stop that? An example:

ob_start();
echo "one";
sleep(10);
echo "two";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海之角 2024-09-25 10:07:38

faileN的答案理论上是正确的。如果没有 ob_flush(),数据将保留在 PHP 的缓冲区中,并且不会到达浏览器,直到请求结束时隐式刷新缓冲区为止。

它仍然不起作用的原因是因为浏览器也包含缓冲区。现在数据已正确发送出去,但浏览器在收到“1”后会等待,然后才真正开始渲染。否则,如果连接速度慢,页面渲染将会非常非常慢。

当然,解决方法(以说明它工作正常)是一次发送大量数据(可能是一些巨大的 html 注释或其他东西)或在命令行上使用像curl 这样的工具。

如果您想在客户端上使用此发送/睡眠周期进行某些状态更新 UI,则必须找到另一种方法(例如长轮询和 AJAX)

faileN's answer is correct in theory. Without the ob_flush() the data would stay in PHP's buffer and not arrive at the browser until the buffer is implicitly flushed at the end of the request.

The reason why it still doesn't work is because the browsers also contain buffers. The data is now sent out correctly, but the browser waits after getting "one" before it actually kicks off rendering. Otherwise, with slow connections, page rendering would be really, really slow.

The workaround (to illustrate that it's working correctly) is, of course, to send a lot of data at once (maybe some huge html comment or something) or to use a tool like curl on the command line.

If you want to use this sending/sleeping cycle for some status update UI on the client, you'd have to find another way (like long-polling and AJAX)

陌上芳菲 2024-09-25 10:07:38
ob_start();
echo "one";
ob_flush();
sleep(10);
ob_start();
echo "two";

你是这个意思吗?

ob_start();
echo "one";
ob_flush();
sleep(10);
ob_start();
echo "two";

Is that what you mean?

离笑几人歌 2024-09-25 10:07:38

如果我理解正确的话,您正在尝试在屏幕上打印部分响应,等待 10 秒并输出其余部分,所有这些都是在页面加载时进行的。这需要一些客户端脚本,因为 PHP 将在最后输出整个响应。

我认为 ob_flush 和 flash 的组合可能会起作用,但是每个浏览器(例如 IE)上的缓冲处理方式并不相同。

If I understand correctly, you are trying to print part of the response on screen, wait 10 seconds and output the rest, all this when the page is loading. This would require some client side scripting for that as PHP will output the entire response at the end.

I think a combination of ob_flush and flush might work, but buffering is not handled the same on every browser (such as IE).

美人骨 2024-09-25 10:07:38

为此,我使用 JavaScript 的 setTimeOut() 函数。效果很好。
此外,对于禁用 JavaScript 的浏览器,您可以使用 标记。

 $txt = setPageHeader();  // a PHP function that returns a new DOCTYPE
                          // plus <html><head>(...)</head>, 
                          // plus an opening <body> tag

echo 'All things were completed. You should be redirected in about 3 seconds';

  $txt .= '<script type="text/javascript">';
  $txt = $txt.'function Rediriger() {document.location.replace(\'http://yoursite.com/yourpage.php?anticaching='.rand().'\');}';
  $txt .= 'setTimeout (\'Rediriger()\', \'3000\')';
  $txt .= '</script>';
  $txt .= '<noscript><a href="http://yoursite.com/yourpage.php?anticaching='.rand().'">Javascript is disabled in your browser. Click here for being redirected.</a></noscript>';
  $txt .= '</body></html>';
  echo ($txt);

I use the JavaScript's setTimeOut() function for this. It works fine.
Additionally, you can use the <noscript> tag for browsers where JavaScript is disabled.

 $txt = setPageHeader();  // a PHP function that returns a new DOCTYPE
                          // plus <html><head>(...)</head>, 
                          // plus an opening <body> tag

echo 'All things were completed. You should be redirected in about 3 seconds';

  $txt .= '<script type="text/javascript">';
  $txt = $txt.'function Rediriger() {document.location.replace(\'http://yoursite.com/yourpage.php?anticaching='.rand().'\');}';
  $txt .= 'setTimeout (\'Rediriger()\', \'3000\')';
  $txt .= '</script>';
  $txt .= '<noscript><a href="http://yoursite.com/yourpage.php?anticaching='.rand().'">Javascript is disabled in your browser. Click here for being redirected.</a></noscript>';
  $txt .= '</body></html>';
  echo ($txt);
舞袖。长 2024-09-25 10:07:38

使用 ob_flush() - 但这会清除缓冲区内容。你不能将延迟注入缓冲区,它只是不能像那样工作。

您可以立即输出整个缓冲区,也可以保留整个缓冲区以供以后使用。

With ob_flush() - but that will clear the buffer contents. You can't inject a delay into a buffer, it just doesn't work like that.

You either output the entire buffer at once, or hold on to the entire buffer for later use.

傲性难收 2024-09-25 10:07:38

不能,因为浏览器正在等待文档的完整版本,因为什么浏览器引擎解析了一半的 XHTML 页面,然后(如何呈现一半的 XML?)读取其他部分。

你必须考虑一下在二进制数据被打磨之前发送标头以通知浏览器,然后浏览器在接收后获取数据,并可能立即在屏幕上显示该数据。

我想了解这个问题,因为我从来没有考虑过注入字符串缓冲区 10 秒睡眠。

Can't because browser waiting for full version of document because what browser engine parsing half of XHTML page and after this (how to render half of XML?) reading other part.

You must think about send header before to inform browser as binary data was sanded then browser get you data after recv and propably get out this data on screen immediate.

I miss understand this question because i never think about inject to string buffer 10s sleep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文