views:

139

answers:

4

I'm writing a PHP script that does a lot of repetitive work, and when the client executes it, I'd like it to send back HTML in some way, dynamically, as it completes tasks. (without AJAX?) How can this be done?

A: 

Then you'd need to refresh the site, since PHP only runs on each page request.

Ólafur Waage
+1  A: 

Maybe you can use refresh-meta tag, or HTTP header, like this:

header("Refresh: 3; URL=http://www.some.org/some.html");

The browser will then refresh after 3 seconds. If you ask me, this can be kinda anying =) It would be much nicer to inclide an AJAX solution, but it will at least work without too much JavaScript work.

Thorbjørn Hermansen
+7  A: 

You can flush the output buffer, using flush();

So something like:

taskOne();
echo 'Task one finished';
flush();
...

Hope this helps.

Wez
If it's a long process, make sure to use set_time_limit() to give it time to complete. If it's so long that the user might leave (but it needs to finish, anyway) then use ignore_user_abort().
James Socol
@Wez and @James: Excellent points and solutions, thank you.
Jenko
Also, be sure you are not using zlib or gzip compression as you cannot flush data to the page.
St. John Johnson
+1  A: 

You're better off just polling the server via ajax. Set up a script that can monitor your running tasks and poll that. That way the users's browser isn't always in loading mode, and they can go away from the page and come back if they want.

rojoca