Jump to content

Recommended Posts

Hi guys !

 

I need to print something before my code issues the sleep command.

But i am not able to achieve this

 

<?php
ob_start();
echo("hello");
ob_flush();
flush();
sleep(10);
?>

 

The code should output "hello" before it issues the sleep command. but unfortunately it prints after 10 secs.

 

Thanks a lot in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/161955-force-flush/
Share on other sites

If you are doing this for your own internal use and will never expect it to work on a different web server/php/browser combination, you might be able to get it to work. However, web servers and browsers are not designed to output content on a single page, piece by piece. You must satisfy, disable, or flush all the buffering, compression, and minimum content length requirements that the php, the web server, and your browser is using. For, example, if your browser indicates it supports compression, your web server will buffer the content until the page ends before sending the output because it must have a minimum amount of output or all of the output before it will complete the compression and send a block. If you are using IE, it won't display a block of information until the length exceeds 256 characters.

Link to comment
https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854539
Share on other sites

I tried this with IE

 

<?php
ob_start();
echo("hello                                                                                                                                                                                                                                                                                                            ");
ob_flush();
flush();
sleep(10);
?>

 

But still i am not getting it...

Link to comment
https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854548
Share on other sites

From the documentation for the flush() function -

 

Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

 

Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

 

Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the </table> tag of the outermost table is seen.

 

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.

 

Your code worked for me, must be your web server. Do you have administrative/root access to your web server so that after you find which module or setting (or even if possible on your type of web server) is causing the problem, you would have the ability to change it?

Link to comment
https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854560
Share on other sites

  • 5 weeks later...

Its one month over now, my last reply was on june 12.. now its july 12th

 

Something strange is happening

 

This code is working fine

 

<?php
for($i = 0; $i < 1; $i++)
{
echo '<script>alert(12340)</script>';
}
ob_flush();
flush();
sleep(50);
?>

 

But this isn't...

 

<?php
for($i = 0; $i < 1; $i++)
{
echo '12340';
}
ob_flush();
flush();
sleep(50);
?>

 

 

How can an echo with alert box flushes properly , but an ordinary echo doesn't

 

:( Please help guys !

Link to comment
https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-874047
Share on other sites

Because browsers render different kinds of content differently.

 

The simple character content is probably subject to a minimum block length before it is rendered while <script></script> is probably not.

 

If you want to waste your time on this, fine, but you were already told this not the correct way to output content to a browser and if you do get it working with your current server/browser, it will probably not work on the next server you try it on and it probably won't work for a great number of visitors to your site due to their browser or browser settings.

Link to comment
https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-874068
Share on other sites

If you want to waste your time on this, fine, but you were already told this not the correct way to output content to a browser and if you do get it working with your current server/browser, it will probably not work on the next server you try it on and it probably won't work for a great number of visitors to your site due to their browser or browser settings.

 

One utility for doing these kind of things would be limiting download speed. Say you want to limit download speed based on which group a user is in (assuming you have some sort of grouped membership system). You can use a combination of fread(), sleep() and flush() to make sure you only send out x byte per second.

 

If you put

for ($i = 0; $i < 10; ++$i) {
echo 'hi<br>';
flush();
sleep(2);
}

in a file you should see the string 'hi' popping up sequentially with a 2 second interval.

Link to comment
https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-874104
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.