Show PHP output continuously?

I have a script that works like 1 hour. I have 2 web hosts, in one of them I see the result in real time (it loops a million times and echoes the sound), and on the other web host, I have to wait until the script is completed, until I see any output in general.

How to change the settings for displaying output in real time? I assume this is php.ini.

Adding code: this code will be displayed at run time on the first server, but not on the second server. Additional information, if I put the value 1015 below, the first server waits with the output. Therefore, it seems that my first server discards all 1015 characters, and the second does not.

while ($a++ < 5) { $b = 0; while ($b++ < 1015) { echo "H"; } echo "<br><br>\n"; sleep(1); } 

On my second server, output buffering is disabled, and I tried to enable implicit_flush to no avail.

+6
source share
2 answers

This is due to output buffering. If output buffering is enabled, the output is not immediately sent to the client, but will wait until a certain amount of data is buffered. This usually increases performance, but causes problems like yours.

A simple solution does not require changing any php.ini parameters, but just put this at the top of your script:

 <?php while (@ob_end_flush()); ?> 

This will immediately disable and disable all output buffering.

(Source: http://www.php.net/manual/en/function.ob-end-flush.php#example-460 )

You can also read the PHP manual on Output Control . The related php.ini settings are listed here .

If you want to make sure things are sent (i.e. before a time-consuming task), call flush() at the appropriate time.


Edit:

In fact, browsers can have their own buffers, especially when the Content-Type is not explicitly set (the browser needs to sniff the MIME file type). If so, you cannot control browser behavior.

One way to solve this problem is to explicitly specify the Content-Type:

 header("Content-Type: text/plain"); // if you don't need HTML // OR header("Content-Type: text/html"); 

But this is not guaranteed. It works on my Firefox, but not Chrome .

In any case, your final code should look like this:

 header("Content-Type: text/html"); // Set Content-Type while (@ob_end_flush()); // Flush and disable output buffers while ($a++ < 5) { $b = 0; while ($b++ < 1015) { echo "H"; //flush(); // In fact because there is no significant delay, you should not flush here. } echo "<br><br>\n"; flush(); // Flush output sleep(1); } 
+6
source

Call flush periodically. If you use output buffering , you can disable it using the ob_end_flush sequence or use ob_flush .

+2
source

All Articles