Problems with ob_get_contents after user abort

One of the easiest and basic ways to handle templating in PHP is to wrap an include(‘template.php’) in output buffering functions such as ob_start() and ob_get_clean(). This allows passing the template contents as a variable for futher processing. It works well in most cases, but there is one catch…
In most cases templates will be used to handle content displayed in a browser, but might be a good choice for formatting data and ie saving it to a file or sending as an email. What’s more – you might want the contents of a giant report to be generated using a process running in the background and then save the results, wrapped into a template to a file. Starting a background process typically involves setting ignore_user_abort(false) and a high or infinite timeout value, to enable it to continue once the caller aborts.
What impact does that have on output buffering? It simply wont work – ob_get_contents() will return an empty string. Once user has aborted, output buffering no longer collects any data as it seems there is nowhere to send it to. The dirty workaround to enable output buffering on background processes is to add a “proxy” process. The proxy process is supposed to call our target process (most likely with Curl) and not timeout as long as the target process is running. As a result only the proxy process recognises that user has aborted.