Last active
December 1, 2022 04:32
-
-
Save koteq/8021622 to your computer and use it in GitHub Desktop.
How to disable output buffering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http/1.1 header to disable browsers cache | |
header('Cache-Control: no-cache'); | |
// tell nginx to disable buffering | |
header('X-Accel-Buffering: no'); | |
// disable apache mod_gzip and mod_deflate | |
apache_setenv('no-gzip', 1); | |
// disable builtin php's gzip | |
ini_set('zlib.output_compression', 0); | |
// it's sad to know, but it's imposible to change output_buffering by using ini_set | |
// ini_set('output_buffering', 0); | |
// tell php to call flush() after each output | |
// ini_set('implicit_flush', true); // seems ob_implicit_flush is better choise | |
ob_implicit_flush(true); | |
// close all started output buffers | |
while (ob_get_level()) { | |
ob_end_clean(); | |
} | |
// here it is, unstopable unbuffered output | |
// but remember to send 512 bytes to browser | |
// or explicitly set page encoding | |
header('Content-Type: text/html; charset=utf-8'); | |
?> | |
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>Buffering Disabled</title> | |
<script> | |
var scrollInterval = setInterval("window.scrollBy(0, 1000);", 100); | |
</script> | |
</HEAD> | |
<head> | |
<h2>Appears first, then waiting</h2> | |
<? | |
for ($i = 1; $i < 6; $i++) { | |
echo "Wait $i sec...<br>\n"; | |
sleep(1); | |
} | |
?> | |
Done! | |
<script> | |
window.scrollBy(0, 1000); | |
clearInterval(scrollInterval); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment