PHP: Output data before and after sleep()?
Asked Answered
P

6

4

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";
Palaeontology answered 10/9, 2010 at 15:7 Comment(0)
D
5

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)

Daugherty answered 10/9, 2010 at 15:52 Comment(1)
I just used a str_repeat of a blank space until 256 bytes, read this would work, and it makes it flawless. Although I need to send \r\n's along with the content before I flush it out for it to work. Thanks for the idea of sending large content.Palaeontology
A
3
ob_start();
echo "one";
ob_flush();
sleep(10);
ob_start();
echo "two";

Is that what you mean?

Antifreeze answered 10/9, 2010 at 15:13 Comment(3)
This still waits until the end to output anything.Palaeontology
try adding this to the beginning of your script: ob_implicit_flush(true);Zr
Did not work, but I got it to work anyway (see comment on pilif's answer), ThanksPalaeontology
D
1

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

Dualism answered 10/9, 2010 at 15:21 Comment(0)
S
1

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);
Sandpaper answered 20/1, 2012 at 17:23 Comment(0)
T
0

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.

Toyatoyama answered 10/9, 2010 at 15:13 Comment(3)
this is not true: php.net/ob_flush - "This function will send the contents of the output buffer (if any)."Daugherty
Yes - AND "as the buffer contents are discarded after ob_flush() is called. " I never said it only cleared the buffer contents.Toyatoyama
this just means that echo "a"; ob_flush(); echo "b" will output "ab" and not "aab". ob_flush() sends out the data that has accumulated in PHP's buffer and then cleans that buffer. But it is sent out.Daugherty
S
0

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.

Solus answered 10/9, 2010 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.