Matt Pfeffer Matt Pfeffer - 10 months ago 28
PHP Question

How to keep executing php script while disconnecting user?

I am looking to have the user submit some information from a form to a page. But I want to do some other stuff after getting the info but I don't want to have the user wait around.
Is there a way to have the user think that the page has finished while still having PHP execute the script?


That is certainly doable. You can use ob_flush() to continue the session. I have an example snippet here:

Pulled from that URL:

This will disconnect the user while the script is still processing

 header("Connection: close");
 ignore_user_abort(); // optional
 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 !
 session_write_close(); // Added a line suggested in the comment
 // Do processing here
 echo('Text user will never see');