How to really, really flush the php buffer?
January 28, 2008 7:44 PM   Subscribe

PHP: How to flush the buffer for really real?

I've got a page where I'm sending the client a small bit of html, but after that's done I want to tell the client that I'm finished with it and then have the server do some database intensive stuff with the information in the url. The problem is that the client waits for PHP to finish executing this time-consuming script before it believes that the page is really finished. I've tried using flush() and ob_flush() together, but this has no effect.

This is a Linux server running php version 4.3.11 on Plesk.

What am I missing?
posted by vraxoin to Computers & Internet (3 answers total)
 
The problem is that the web server won't close the connection until the script terminates.

It sounds like what you're trying to do would be better handled by a cron process. Alternately, you could fork a background process that keeps running after the web script has finished, but you need to be careful not to clobber your server with hundreds of zombie processes.
posted by justkevin at 7:52 PM on January 28, 2008


You should look into the comments on connection handling and specifically ob_end_flush(). Depending on the webserver, you may not be able to accomplish the behavior that you are seeking.
posted by tumble at 7:58 PM on January 28, 2008


In case anyone stumbles upon this thread, here was the answer that worked for me, from the link on connection handling provided by tumble, above:

<> ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
echo ('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here
sleep(30);
echo('Text user will never see');
?>
posted by vraxoin at 8:19 PM on January 28, 2008


« Older Advice before interviewing for small environmental...   |   How to best sell old collectables? Newer »
This thread is closed to new comments.