php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64998 php-cgi is buffering output
Submitted: 2013-06-09 03:08 UTC Modified: 2013-06-09 05:04 UTC
From: m00n dot silv3r at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.16 OS: Ubuntu, Centos
Private report: No CVE-ID: None
 [2013-06-09 03:08 UTC] m00n dot silv3r at gmail dot com
Description:
------------
Here is the program
-----------------------------------
ini_set('output_buffering', 0);

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

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event 
data.

//Should output step by step in realtime
for($i = 0; $i < 10; $i++)
{
	echo sleep(1) . 'Ting';
	
	ob_flush(); flush();
}
--------------------------------------

When opened in browser, it should output data in realtime, with each echo 
statement. However it is outputting only after script execution completes.

Tested on Apache 2.2.22 + Php running as cgi, and with mod_fastcgi
Php CLI works as expected.

This happens on Php 5.4.x


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-09 03:47 UTC] rasmus@php.net
-Status: Open +Status: Analyzed
 [2013-06-09 03:47 UTC] rasmus@php.net
I doubt there is a bug here. Your data is likely being buffered by your web 
server, most likely due to mod_deflate being enabled.
 [2013-06-09 04:14 UTC] m00n dot silv3r at gmail dot com
No mod deflate or mod gzip is there.
Apache does not appear to be buffering the output.

This is further confirmed when debugging with telnet. When opening the page with 
telnet, the output comes in chunks of 4K. This indicates that its php that is 
buffering the content.
 [2013-06-09 04:20 UTC] rasmus@php.net
I don't see how. CGI is pretty simple. It just writes to stdout and when you call 
flush() cgi does fflush(stdout). If you are seeing 4k buffers, it isn't coming 
from PHP. Try it without your web server. Emulate CGI by setting the right env 
variables (see the CGI spec) and you will see there is no buffering.
 [2013-06-09 04:50 UTC] m00n dot silv3r at gmail dot com
Following has been tested to be working fine

PHP Version 5.4.4-14 + Debian + Apache 2.2.22 + Cgi

Output comes in realtime as expected.
 [2013-06-09 04:56 UTC] rasmus@php.net
So what exactly is your bug report?

Are you saying that:
PHP 5.4.x + Debian + Apache 2.2.22 + Cgi is fine
but
PHP 5.4.x + Ubuntu and Centos + Apache 2.2.22 + Cgi is not?
 [2013-06-09 04:57 UTC] m00n dot silv3r at gmail dot com
This setup is HAVING ISSUE

Php 5.4.6 + Ubuntu 12.10 + Apache 2.2.22 + Cgi

------

Same machine, This setup is NOT HAVING ISSUE

Php 5.4.6 + Ubuntu 12.10 + Apache 2.2.22 + mod_php

------

Can this conclude that apache is not buffering the output ? If it indeed were, 
thatn mod_php should too have the issue ?
 [2013-06-09 05:04 UTC] rasmus@php.net
-Status: Analyzed +Status: Not a bug
 [2013-06-09 05:04 UTC] rasmus@php.net
But you just said a similar CGI setup on Debian worked fine. To me that says it 
is something in your Ubuntu CGI config. Either way, the only thing PHP needs to 
do is flush stdout, and it does that. Once PHP has flushed stdout it is up to the 
web server to deliver this to the browser.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 26 08:01:31 2024 UTC