Skip to content

Instantly share code, notes, and snippets.

@pjbeardsley
Last active December 12, 2015 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjbeardsley/4716592 to your computer and use it in GitHub Desktop.
Save pjbeardsley/4716592 to your computer and use it in GitHub Desktop.
PHP: long running process with closed connection
<?php
set_time_limit(0);
ignore_user_abort(TRUE);
// return the headers and close the connection, but keep running.
ob_start();
fJSON::output(array('success' => 'T', 'message' => 'The full synchronization process has started.'));
$length = ob_get_length();
header("Content-Length: $length");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
// if you're using sessions, this prevents subsequent requests
// from hanging while the background process executes
if (session_id()) session_write_close();
// do work...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment