views:

218

answers:

4

Hi all!
I've a php script that only produces logs to the client.
When I echo something, i wan't it to be transfered to client on-the-fly.
(Because while the script is processing, the page is blank)
I had already played arround with ob_start() and ob_flush(), but they didn't worked.

What's the best solution?

PS: it is a little dirty to put a flush at the end of the echo call...

EDIT: Neither the Answers worked, PHP or Apache Fault?

Thanks in advance, José Moreira
Sorry for my bad English. ;)

+3  A: 

what you want is the flush method. example:

echo "log to client";
 flush();
GSto
it is a little dirty to put a flush at the end of the echo call...
CuSS
@CuSS Regardless, this is the only way to satisfy the functionality that you want
Justin Johnson
what's dirty about it? That's what flush() was designed to do, and it gets the job done.
GSto
`ini_set('implicit_flush', true)` will basically emulate a flush() after ever output block, and it can get set pretty much anywhere (php.ini, .htaccess, per-script, etc...)
Marc B
+1  A: 

The correct function to use is flush().

<html>
<body>
<p>
Hello! I am waiting for the next message...<br />
<?php flush(); sleep(5); ?>
I am the next message!<br />
<?php flush(); sleep(5); ?>
And I am the last message. Good bye.
</p>
</body>
</html>

Please note that there is a "problem" with IE, which only outputs the flushed content when it is at least 256 byte, so your first part of the page needs to be at least 256 byte.

moontear
Cool, didn't knew about that IE Bug... Is that any function besides `echo` and `print` that prints directly to the browser?
CuSS
@CuSS: It's not a bug; it's an "undocumented feature"
amphetamachine
+2  A: 

Why not make a function to echo, like this:

function fecho($string) {
 echo $string;
 ob_flush();
}
cam8001
Better that @GSto Answer, but I'm searching for something like @amphetamachine Answer... You will get +1 ;)
CuSS
+1  A: 

Edit:

I was reading the commends on the manual page and came across a bug that states that ob_implicit_flush does not work and the following is a workaround for it:

ob_end_flush();

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_start();

What may even be happening is that the client does not receive the packet from the server until the server has built up enough characters to send what it considers a packet worth sending.


Old Answer:

You could use ob_implicit_flush which will tell output buffering to turn off buffering for a while:

ob_implicit_flush(true);

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_implicit_flush(false);
amphetamachine
Yes, definitely I'm looking for something like that (+1), but it didn't worked :s Can it be Apache's fault?
CuSS
These two comments to that function may help: http://www.php.net/manual/en/function.ob-implicit-flush.php#35072http://www.php.net/manual/en/function.ob-implicit-flush.php#33778
cam8001
If i stop the flush `ob_end_flush()`, my program skips the important step (don't know why ;s)I've added the line `flush` on my Class `DebugEcho` function, and it didn't workedtoo, besides, it's a little bad and time consuming doing a flush on the cache every `echo` call.
CuSS
ob_end_flush will clear the buffer and stop output buffering completely, so don't use that until you're sure that you don't want to buffer anymore content!
cam8001
You still need to `flush()` after each line of output as it is not guaranteed to be sent to the client immediately.
Justin Johnson