开发者

PHP: Output data before and after sleep()?

This is p开发者_如何学Curely 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";


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)


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

Is that what you mean?


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜