views:

55

answers:

3

Hi, I need to display continuous output from my php script. The script will stay in a loop of printing log data from a device and sleeping. My problem is that I can't get PHP or Apache to output data before the script is completed.

I have tried all the commands for flushing the output buffer, and I have read through the config-files for both PHP and Apache looking for some type of buffering to disable.

Is this possible to do with Apache and PHP on Windows, or should I consider some other platform?

+1  A: 

Use these flushing the output buffer commands you've used already, and throw in an HTML comment, 1024 bytes long. Cause it's most likely browser issue, not server.

Ahh, looks like you're looking for the COMET application

http://www.zeitoun.net/articles/comet_and_php/start

Col. Shrapnel
+2  A: 

The best way (most reliable and clean) to accomplish this is to use a server-side script which does the work, and a client-side AJAX script which will poll another server-side script which will return the status. In the web, data is buffered at different levels, to achieve optimization, and therefore it is difficult to accomplish what you want.

Palantir
Yeah, I have thought about client polling at regular intervals, but I was trying to avoid unnecessary lag, but I will go for that approach if nothing else is possible.
Torstein
On the contrary, this should give your client a better performance, not a worse one: if you follow your approach, you are never certain when will the text be output, because it will be dependent on the browser's implementation. On the contrary with the AJAX approach, if you poll at 1sec interval, you will be sure that the client will be updated each 1 second, which should be good enough for any web user.
Palantir
Ok, makes sense. I will try out that approach, thanks.
Torstein
A: 

PHP's output_buffering setting is 0 by default (i.e. off), but you've no doubt investigated that.

I think @Palantir's suggestion is probably a good one: write the log entries to a local file, and then serve up that file (or parts thereof) to the client; the client can re-request however often you desire. This also has the advantage of not filling up the browser's buffer (i.e. you shouldn't just keep sending log data to the client ad infinitum).

But that wasn't your question.

Can you reproduce the output buffering problem with a simple script like the following?

<?php
while (TRUE)
{
    echo 'x';
    flush();
    sleep(1);
}
?>
Sam
I have tried a simple script like that, but it won't give me any output.
Torstein