php 5.3 causing flush() to fail? - 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.

Related

PHP flush not working on centOS with Apache

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!

PHP terminates script unexpectedly

I'm working in PHP and creating a system with a lot of PHP-driven elements and I have noticed that some of my pages stop displaying text produced using the echo command.
I have made a small example of this. Of course, my program is not supposed to just print allt numbers from 1 to 10000, but this example demonstrates how the script just terminates without any warnings.
Example code:
<?php
for ($i = 1; $i <= 10000; $i++) {
echo $i, '<br>';
}
?>
Output:
1
2
More numbers...
8975
8976
8977
8
What is causing this? Is it a buffer issue, and how do I resolve it?
The fact that your code ran to completion on the cli suggests to me that your script is exceeding the ini.max_execution_time runtime configuration.
Note in the linked documentation that the value of this configuration on the cli is 0 which means it does not time out when run in that environment.
The default setting in the browser is 30 seconds.
You can show your current setting in the browser with:
echo ini_get('max_execution_time');
And you should be able to increase it with:
ini_set('max_execution_time', 0); // turns off timeout
If the script you have shown us behaves as you describe the there's something very wrong going on. If this is a Unix or Linux based system and its repeatedly exhibiting this behaviour then the kernel is terminating the script - unless it has been configured not to do so, the kernel will be forcing a core dump of the process.
Either go build a new system to run your code on or Google how to capture and diagnose a core dump on your operating system.
update
If xdebug is reporting the process is still running then it probably hasn't dumped its core, but "not producing output" != "not running". What state is the process in? What happens when you redirect the ouput? What is the end-to-end output channel when it misbehaves?
The problem did not lie directly with my PHP installation or the application itself, but somewhere in my IDE PHPStorm. When running the code with the same PHP interpreter outside of the IDE's wrappers, it all works fine. The procedures described by the many users here helped with that. Thank you.

PHP 5.4.24 timeout due to ob_start()

I am trying to run this set of code
public function getDataCellContent($row,$data) {
ob_start();
$this->renderDataCellContent($row,$data);
return ob_get_clean();
}
When it hits the ob_start() line it just hangs and times out after 30 seconds. I am having this issue on Mac Osx Mavericks. I have an ubuntu server running the same code without this issue happening. Is something messed up with my ini file or is php 5.4.* known to have this issue?

PHP script not echo'ing logs in real time - Was working on my other server

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!

AjaXplorer [written in PHP] is too slow on IIS

I've installed AjaXplorer (very nice web file explorer), written in PHP, on my IIS (Windows Server 2008 SP2 x64). It works too slow for me.
What can be the cause? Are there some settings in php.ini? Or, maybe, something is wrong with IIS?
I use 32-bit PHP, php-cgi.exe as interpreter.
Regards,
First off, CGI will always be slow. It needs to boot the entire PHP runtime for each request. Try using FastCGI (If you're using IIS 7, or if you're using IIS 6)...
After that, try to see why it's slow. Is it because the PHP script takes a long time to execute (meaning it's a code issue), or is it because of a server config. To test, modify this into the start of the entrance point of the PHP program (index.php):
define(START_TIME_CUSTOM, microtime(true));
function onEndTimeCompute() {
$timeTaken = microtime(true) - START_TIME_CUSTOM;
echo "Completed In: ".number_format($timeTaken, 4)." Seconds\n";
}
register_shutdown_function('onEndTimeCompute');
That write Completed in n Seconds to the end of the generated output (even if die() is called). It may cause some issues if Ajax calls are expected to return JSON, so don't do it as a rule, just for trying to figure out what's going on.
So, if the total request takes 1 second, yet you see Completed in 0.004 Seconds, you know that the PHP code itself is not the issue (it's either in the setup of the interpreter by CGI, or somewhere else in IIS)...
That should at least show you where the problem is...

Categories