d.shankar Posted June 12, 2009 Share Posted June 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854539 Share on other sites More sharing options...
d.shankar Posted June 12, 2009 Author Share Posted June 12, 2009 I tried this with IE <?php ob_start(); echo("hello "); ob_flush(); flush(); sleep(10); ?> But still i am not getting it... Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854548 Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854560 Share on other sites More sharing options...
d.shankar Posted June 12, 2009 Author Share Posted June 12, 2009 Thanks for checking my code from your end. I dont have admin access on the webserver, what settings should i look for ? Thanks for your efforts. Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854564 Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 Anything that affects buffering and compression, varies with your web server type, version, and how it has been setup. Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854578 Share on other sites More sharing options...
d.shankar Posted June 12, 2009 Author Share Posted June 12, 2009 Thank you for your help friend. I will check out and let you know. Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-854584 Share on other sites More sharing options...
d.shankar Posted July 12, 2009 Author Share Posted July 12, 2009 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-874047 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2009 Share Posted July 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-874068 Share on other sites More sharing options...
d.shankar Posted July 12, 2009 Author Share Posted July 12, 2009 thanks friend. is the problem with the browser or the webserver ? cause i have tested this code in all browsers and the same happens.... Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-874093 Share on other sites More sharing options...
Daniel0 Posted July 12, 2009 Share Posted July 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161955-force-flush/#findComment-874104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.