Skip to content

Instantly share code, notes, and snippets.

@ahamilton9
Created February 12, 2021 16:25
Show Gist options
  • Save ahamilton9/ff1115c221fba04a9d0c41c86aeb3c66 to your computer and use it in GitHub Desktop.
Save ahamilton9/ff1115c221fba04a9d0c41c86aeb3c66 to your computer and use it in GitHub Desktop.
Respond early to a PHP request and keep processing
<?php
// Provide a success response early
// Modified from: https://stackoverflow.com/a/42245266
function respondEarly() {
if(session_id()) {session_write_close();}
if(is_callable('fastcgi_finish_request')) {
fastcgi_finish_request();
return;
}
ignore_user_abort(true);
ob_start();
$serverProtocol = filter_input(INPUT_SERVER, 'SERVER_PROTOCOL', FILTER_SANITIZE_STRING);
header($serverProtocol . ' 200 OK');
header('Content-Encoding: none');
header('Content-Length: ' . ob_get_length());
header('Connection: close');
ob_end_flush();
flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment