Joel
Joel

Reputation: 895

How to render pages as we go in PHP?

It's really weird, I just changed server last week and I have a large script that process lots of information (personal script).

What it does it add stuff to the database with a foreach and then echo where it is (ex: "ID #27 was processed") and so on until its done. With the old server, I would see this "echo" script run one by one but with my new server, it shows a blank page with the hourglass until it's loaded/done.

Is there a config on the server or PHP that can show me the page as it is constructed?

Thanks

Upvotes: 2

Views: 1969

Answers (4)

Michael Berkowski
Michael Berkowski

Reputation: 270637

If you have access to the php.ini, you can set output_buffering = Off to disable this behavior entirely. Otherwise, use flush() as suggested by others.

I believe the default setting for output_buffering is 4096. If the new server is set much higher, you might also try simply lowering it rather than turning it off to achieve similar behavior to your old server without losing buffering entirely.

EDIT According to PHP docs, Apache mod_gzip may do its own output buffering which prevents flush() from working.

Upvotes: 4

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

I assume you push the output to the client using flush(). There are quite a few notes on flush on the php page, see the description.

For example: flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser..

This could instantly explain the entire situation. Can you validate these notes aren't the cause?

Upvotes: 0

steveo225
steveo225

Reputation: 11882

Try using the flush and/or ob_flush after the echos, you output stream may be buffered.

Upvotes: 2

Waldheinz
Waldheinz

Reputation: 10487

Calling flush after each relevant output might help.

Upvotes: 2

Related Questions