PHP 실시간 버퍼링 없이 브라우저 출력

1 개념[ | ]

  • 프로그램에서 브라우저까지 출력되는 과정을 이해함
  • 프로그램에서 브라우저까지 실시간으로 출력 할 수 있는 방법

2 출력 과정[ | ]

프로그램 출력 → PHP → Apache/Web server → Browser
  • echo 를 통해 프로그램에서 출력을 하게되면 브라우저에서 곧장 출력되기를 기대
  • 하지만 버퍼링 등의 문제로 기대와는 달리 곧장 출력이 되지 않음

3 방법[ | ]

  • PHP 버퍼 비활성화
  • 아파치 버퍼 비활성화
  • 캐쉬 비활성화
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);

//Flush (send) the output buffer and turn off output buffering
while (@ob_end_flush());

// Implicitly flush the buffer(s)
ini_set('implicit_flush', true);
ob_implicit_flush(true);

//prevent apache from buffering it for deflate/gzip
header("Content-type: text/plain");
// recommended to prevent caching of event data.
header('Cache-Control: no-cache');

ob_start();
for ($i = 0; $i < 100; $i++) {
	echo $i;
	echo str_pad("", 4096);
	sleep(1);
	ob_flush();
	flush();
}
ob_end_flush();

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}