NYCPHP Meetup

NYPHP.org

[nycphp-talk] Processing, please wait logic flow question

Dell Sala dell at sala.ca
Mon Oct 30 22:12:18 EST 2006


Cliff,

I believe the most common solution is to have the your processing  
script launch a sub-process to run in the background, and then return  
a response page that periodically refreshes (via javascript) to poll  
the server for some flag indicating that the process has completed.  
(Ex: checking for the existence of a file or something like that).

You launch the sub-process using exec() or some other function in  
that family. http://us3.php.net/manual/en/ref.exec.php

The trick with these functions is that the calling script halts when  
the function is called and waits for the sub-process to complete and  
return a result. You can prevent this by setting the sub-process to  
run in the background and redirecting its output (STDERR _and_  
STDOUT) to go to a file (or nowhere).

Here's an example:

exec("php takes-a-long-time.php > /dev/null 2>&1 &");

takes-a-long-time.php would do something time-consuming, and when it  
completed, would deposit a file somewhere or add a line to a file to  
indicate that it was complete.

There are lots of related comments in the user contributed notes on  
the exec page that I linked to above. I guess there are some  
potential problems with trying to do this on a windows server -- I've  
only done this on unix servers.

The other gotcha I've run into is that that php goes CRAZY if you try  
this on a server that is running php as a CGI. If you call a php sub- 
process from a php page it goes into a recursive loop and eventually  
chokes. I'd be curious to hear if others have run into this problem.  
Any solutions?

***

I believe there is another way to tackle this issue which involves  
some fancy output buffering. The page only partially downloads and  
then keeps the connection open while it waits for the rest of the  
script complete. Anyone else familiar with this technique?

-- Dell


On Oct 30, 2006, at 9:18 PM, Cliff Hirsch wrote:
> Many web sites display a “processing please wait” page after  
> submitting an order, request, etc. and then display the final  
> confirmation page when it’s available.
>
> I’m confused by how that works. Does the server-side script spit  
> out a “processing” page by flushing the output buffer and then  
> redirect when the script is completed? Or does the client-side  
> JavaScript display the “processing” page while the server script  
> goes about its business?
>
> Cliff




More information about the talk mailing list