Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Created August 20, 2017 16:34
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 Pamblam/62aa44b7890b9bb0b2c1a3d5db1d424b to your computer and use it in GitHub Desktop.
Save Pamblam/62aa44b7890b9bb0b2c1a3d5db1d424b to your computer and use it in GitHub Desktop.
A class to trigger long running processes from the browser

asyncPage

asyncPage has 2 static methods.

Call asyncPage::startOutput() before sending anyoutput back to the client. This will start the output buffer.

Call asyncPage::sendOutput() after sending any output to the client. This will close a connection to the browser and allow you to run a long-running task afterwords without holding up the browser.

<?php
/**
* The basic class for handling async PHP requests
* @author Rob Parham 10/26/15
*/
class asyncPage{
/**
* To be called that the start of any page that is intended to
* return content to the browser and close connection before
* stopping excecution.
*/
public static function startOutput(){
ignore_user_abort(true);
set_time_limit(0);
ob_start();
}
/**
* To be called after sending output back for the browser and before
* executing any long-running code
*/
public static function sendOutput(){
$contentLength = ob_get_length();
header("Content-Length: $contentLength");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
if (session_id()) session_write_close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment