Saturday, July 7, 2012

php post response script execution

I guess it's nice to have so here you go


/*
 * basically allow a php script to return a response and continue with
 * code execution, good for statistics.
 * before echo anything to user call begin and after call end, than you can continue doing stuff
 */
class ScriptContinuationManager
{
    /**
     * this is the point where we need to give a sign to this class
     * that we wanna write our response.
     * @return void
     */
    public function beginRespone()
    {
        ob_end_clean();
        header("Connection: close");
        ignore_user_abort(); // optional
        ob_start();

    }

    /*
     * after this function execution the response will be sent to the
     * client, and code continue without client need to wait.
     */
    public function endResponse()
    {
        $size = ob_get_length();
        header("Content-Length: $size");
        ob_end_flush(); // Strange behaviour, will not work
        flush(); // Unless both are called !

    }
}