This snippet:
ob_start();
for($i=0;$i<70;$i++)
{
echo 'printing...<br />';
ob_flush();
flush();
usleep(300000);
}
From this page: http://www.php.net/manual/en/function.flush.php#85382
Isn't working on WAMP2 (PHP 5.3.0, Apache 2.2.11) installed on Windows 7, browsing from http://localhost with IE 8, FF 6.0.2 and Chrome 13.
None of them worked. All the 'printing...' lines are just output in one instant batch when the page finished processing.
output_buffering in php.ini is set to 'On'.
Any ideas why it isn't working?
Several reasons why flush fails are discussed on the ob_flush() documentation page (eg interference with certain antivirus sw's, interference with zlib compression, ...). Worth reading.
You may need to close the session:
echo 'printing...<br />';
session_write_close();
ob_flush();
flush();
Related
I am trying to create a console for a script I'm working on, and make the progress stream in so the user can see it.
I read that having output_buffering disabled will not allow this to happen. So, I disabled it on my localhost server... I tried:
output_buffering = Off
output_buffering = 0
And commenting the line out although.
The console still works properly on localhost. Then I read that "gzip" won't allow this to happen. But gzip is enabled on both localhost and online server.
gzip compression enabled
zlib.output_compression is also Off
I even used diffchecker to change both the phpinfo(), and changed my localhost to be the exact same settings, yet it STILL works on localhost.
The only difference I can see is that my localhost is using CGI, and my server is using FastCGI.
Is this what's causing it not to work, and if not, what else would cause it to work on localhost, but not online?
Here's the basic code (it's being used with Ajax, but, I can't even make this basic version work):
<?php
ob_start();
echo "Starting...";
ob_flush();
flush();
echo "Finished with X";
ob_flush();
flush();
sleep(2);
echo "Finished with Y";
ob_flush();
flush();
sleep(2);
echo "Finished with Z";
ob_flush();
flush();
sleep(2);
?>
After spending forever reading threads, it seems that it was a problem with FastCGI and PHP. This code works though, even though I don't quite understand it.
if (ob_get_level() == 0) ob_start();
echo str_pad('',1024*64);
ob_flush();
flush();
usleep(500000);
I have a centOS 8 server with Apache FPM/FastCGI as webserver.
I want the output to be seen right after each echo.
I tried setting output_buffering = Off in php.ini and used below code:
ob_implicit_flush();
ob_start();
echo '1';
ob_end_flush();
#ob_flush();
flush();
sleep(5);
echo '2';
but page remains on loading for 5 seconds and outputs 12 instead of outputting 1 then 2 after 5 seconds.
I used fastcgi_finish_request() to output the whole buffer and do stuff in background and it works correctly.
And also, gzip and deflate are both turned off.
I use above code on my local Windows machine with Apache and also on another server with LiteSpeed and they work without a problem.
Your help is much appreciated!
I recently migrated from Amazon Elastic Beanstalk to my own ubuntu 14.04 lts server. Everything ported smoothly except one page that uses ob_flush and proceeds to do processing. Here is that block of code:
<?php
//put string of page ----> $string
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // optional
ob_start();
echo ($string);
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
session_write_close(); // Added a line suggested in the comment
// Do processing here
sleep(10);
//do stuff
echo "something";
I'm expecting the contents of $string to print, instead the contents of $string, and then "something" print. In this snippet, "something" would still print. I've disabling mod_pagespeed (which is currently enabled), and made sure gzip, and buffer output were set to off.
Here is my php.ini file (I have it offsite because it's unabridged and possible not relevant to the question). I also have a suspicion this may have to do with my apache configuration file which you can see Here. Finally, if this has anything to do with my issue, here is a link to my pagespeed configuration. When I call the function the error log shows no errors, additionally I have root access to the server. Thank you for helping me fix this issue!
Jake Sylvestre
I think that you are confusing ob_get_contents and ob_end_flush see: http://php.net/manual/en/function.ob-start.php.
I have two Linux hosting plans with godaddy. One (older runs) php 5.2.14, newer runs php 5.3.6. I use Firefox 11.0 and IE 9 on Vista.
PHP below runs ok with php 5.2.17 (numbers appear every half second) until the script finishes. Hosting that uses PHP 5.3.6, will not work. It loads for a while, then flushes the entire output at once after the script is done. No intermediate output.
php.ini has output_buffering = Off and zlib.output_compression = Off. Does anyone know how I can make this work under PHP 5.3?
Thanks,
emmets
<?php
$i = 0;
while ($i<=10)
{
echo “i=$i “;
flush();
echo(str_repeat(‘ ‘,1024));
usleep(500000);
$i++;
}
?>
https://bugs.php.net/bug.php?id=49816
It seems to be a known bug.
I wasn't sure how to title this thread, sorry.
I have a script that processes some logs and I echo a lot of debug information as the process goes. Since moving to the new server, it seems that the script hangs for 30 odd seconds, then spits out all the logging, then hangs again for 30 odd seconds and the process continues.
This is really odd behavior and I don't know where to start. Its like it isn't processing the file line by line but in blocks ...
PHP version is 5.1.6 on a CentOS running plesk. (My old CP was CPanel)
Any ideas?
EDIT: Simple example of my issue - Running this code:
for ($i=0; $i<100; $i++) {
echo "test $i";
sleep(1);
}
the script will hang for 100 seconds, then print out all the "test 1" ect. Sleep is required in my main script and on the other server just echoed the values in turn.
EDIT2: Have tried setting output_buffering = 0 and implicit_flush = On and didn't help.
You may have output_buffering On. Try to disable it first.
You can do it either in the php.ini file, in a .htaccess file if your server allows it, or use the following code at the beginning of your PHP script:
while (ob_get_level()) ob_end_clean();
Also, use flush() after each echo or print, and it should be all right!
Update:
You might also encounter other buffers that you cannot control from within PHP (web server, browser, ...), which is why you're still not seing anything. A workaround is to send some blank bytes after each print:
while (ob_get_level()) ob_end_clean();
for ($i=0; $i<100; $i++) {
echo "test $i";
echo str_repeat(' ', 256);
flush();
sleep(1);
}
However, while this example works for me on IE & Firefox, it does not work on Chrome!