This simple function outputs a string and closes the connection. It considers compression using "ob_gzhandler"
It took me a little while to put this all together, mostly because setting the encoding to none, as some people noted here, didn't work.
<?php
function outputStringAndCloseConnection2($stringToOutput)
{
set_time_limit(0);
ignore_user_abort(true);
if(!ob_start("ob_gzhandler"))
ob_start();
echo $stringToOutput;
$size = ob_get_length();
header("Content-Length: $size");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
if (session_id()) session_write_close();
}
?>