ignore_user_abort(true);
set_time_limit(0);
ob_start();
// do initial processing here
echo "content 1"; // send the response
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();
//do expensive processing here (user will never see this)
echo "content 2";
what this do is, to make php close connection early so user will not wait longer when the server do expensive processing.
If i refresh browser multiple times, sometimes it shows “content”, sometimes it shows “content 1”. Why is that?
If I turn off my cloudflare and refrsh the page multiple times it always shows “content 1” all the time.
I am afraid I have to spill the beans, just because something is on StackOverflow does not make it automatically right. Not only is the linked response seven years old (the question itself nine years) that very “solution” got quite a few comments about it not properly working.
The correct approach on how to implement that is the one actually marked as solution in that thread.
What you copy/pasted here is very unlikely to work reliably, particularly not in a proxied context.