BastiaanWW
BastiaanWW

Reputation: 1299

Why is PHP flush not always working, page 1/5 times not loaded properly?

A large php script uses flush to send a part of the generated html to the browser while it is executing the larger part of the script which takes appr. 20 seconds.

The part of the script that is being executed after the flush is pretty large and takes a lot from the server reserves (90% cpu).

Once in 5 times the page stays blanc (white) which means that the flush didn't arrive to send the image that shows the page is loading. The page stays blanc (browser indicates it is still loading) also when the program is finished and should send the whole page to the browser.

Remarkable: When I press the back button the whole page would show (that should have been loaded in the browser after whole script is executed) for a second and then the browser goes back to the previous page.

All the other 4 times the page loads well. The input for the php script is the same for all 5 times.

I have these settings in my .htacces file:

addhandler x-httpd-php5-cgi .php5 
addhandler x-httpd-php5-cgi .php5
addhandler x-httpd-php5-cgi .php5 
Header Set Cache-Control "max-age=0, no-store"

This is the statement to flush the generated code to the browser:

print str_pad('',4096)."\n";
ob_flush();
flush();

What could be causing this problem?

Upvotes: 5

Views: 496

Answers (2)

goat
goat

Reputation: 31813

print str_pad('',4096)."\n";

did you notice you're repeating an empty string? Maybe you meant

print str_pad(' ',4096)."\n";

the purpose of that statement is because some web browsers will not render the page until a certain amount of bytes have been received, or the response is complete, whichever comes first.

Upvotes: 2

Wrikken
Wrikken

Reputation: 70460

You can only hint: the flush() sends it to apache/webserver, which can buffer/wait, which sends it to the network with any number of proxies which can buffer/wait, which ultimately ends up in your browser, which can also decide to buffer/wait. Go for an asynchronous method if you don't want to deal with all that those headaches. Gearman makes it very easy, but not strictly necessary of course.

Upvotes: 2

Related Questions