I elaborated following functions, so you can close your connection if you can't count on fastcgi_finish_request.
You'll use puts instead of echo, so you can close the connection whenever you want as long as you don't touch echo when you use those functions.
It works it compression and is easy to adapt:
function connsettings($name, $val=null){
static $settings = array();
if(!isset($settings[$name]))
$settings[$name] = false;
if($val !== null)
$settings[$name] = $val;
return $settings[$name];
};//Just saving compression and init var to check if it's true or false
function puts(){
//Using args so it works like echo with comma ands dots
$numargs = func_num_args();
$arg_list = func_get_args();
$string = '';
if($numargs === 0)
return;
else if($numargs > 1)
for($i = 0; $i < $numargs; $i++)
$string .= $arg_list[$i];
else
$string = $arg_list[0];
if(connsettings('init') === false)
{
// buffer all upcoming output - make sure we care about compression:
if(ob_start("ob_gzhandler"))
connsettings('compression', true);
else
ob_start();
connsettings('init', true);
register_shutdown_function('puts', null, true);
}
echo $string;
}
function close(){
if(!headers_sent())//it may work without this verification when no compression but may lead to uncomplete data
{
// send headers to tell the browser to close the connection
ob_end_flush(); // Order here matter, it won't work if it goes after Content-Length
if(connsettings('compression') === false)
header("Content-Encoding: none");
header('Content-Length: ' . ob_get_length());
header('Connection: close');
// flush all output
//ob_end_flush();
ob_flush();
flush();
// close current session
if (session_id()) session_write_close();
set_time_limit(0);
ignore_user_abort(true);
}
}