2

Possible Duplicate:
close a connection early
How to continue process after responding to ajax request in PHP?

I need to drop connection in ajax actions:

$response = getResponse($params);
echo $response; // don't make user to wait
flushAndDropConnection();
someLongActionsFor5Seconds();

How can I do it?

Community
  • 1
  • 1
Dmitry
  • 7,457
  • 12
  • 57
  • 83
  • 3
    possible duplicate of [How to continue process after responding to ajax request in PHP?](http://stackoverflow.com/questions/1481247/how-to-continue-process-after-responding-to-ajax-request-in-php) or http://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection or http://stackoverflow.com/questions/138374/close-a-connection-early or – Manse Jul 30 '12 at 13:52
  • You should read discussions in the PHP documentation for `flush()`. This post specifically: http://www.php.net/manual/en/function.flush.php#91556 – Tchoupi Jul 30 '12 at 13:54
  • The solution in duplicate doesn't work. – Dmitry Jul 30 '12 at 14:08

1 Answers1

2

Just make the user disconnect, but leave the script running.

ignore_user_abort(true);
ob_start();
echo "response";
$size = ob_get_length();
header("Content-Length: $size");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
if (session_id()) session_write_close();
//your long running action here
iblue
  • 29,609
  • 19
  • 89
  • 128