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
2009-05-14 13:15:51
+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
2009-05-14 13:23:31
+7
A:
You can flush the output buffer, using flush();
So something like:
taskOne();
echo 'Task one finished';
flush();
...
Hope this helps.
Wez
2009-05-14 13:32:30
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
2009-05-14 13:38:10
@Wez and @James: Excellent points and solutions, thank you.
Jenko
2009-05-14 18:20:50
Also, be sure you are not using zlib or gzip compression as you cannot flush data to the page.
St. John Johnson
2009-05-14 21:21:08
+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
2009-05-14 21:25:01