How to send data a little at a time?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a script that makes multiple requests to several sites.  However,  
each request takes a little while.  How can I get the PHP script to  
output data to let the user know that it's checking?  For example, I have

echo "Checking...";

at the beginning, before I do any hocus-pocus, but it doesn't show up  
until the script is done.  How do I get it to send some data at a time?

Re: How to send data a little at a time?

Quoted text here. Click to load it

This can be a little difficult.  For certain browsers they will not  
display anything until there is a certain byte size reached.  However  
there are a few ways... I will take you through a few of them.

1) Create an image that tells them it is processing when they click it,  
show it when the user clicks submit and then hide it on completion.

2) The AJAX way
If you want to make an AJAX request it is far easier than flushing the  
output.  For instance if you use AjaxRequest (, you  
could use the onLoading to display the contents as they are flushed to  
the browser, then onSuccess when it is finished.  See the examples for  
details on how to implement this.

3) The harder way (requires some research)
You can use flush from php, but this doesn't always work correctly.  See  
the manual for an explaination ( )

I would suggest using AJAX as you can take that content and replace it  
into the current area after you have processed it.

Mike Willbanks
Zend Certified Engineer

Re: How to send data a little at a time?

Hash: SHA1

Mike Willbanks wrote:

Quoted text here. Click to load it
Quoted text here. Click to load it

4) The threaded way

Use pcntl_fork() to create a child process (or, if posible, a thread) for
each site request. As all the children share the same stdout file
descriptor, you should be able to echo() text to the browser asyncronously
(or, you could use popen() or some other pipe mechanism to centrlize the
However, this approach has the disadvantage of potentially becoming a
concurrency hell. Make sure you have the proper inter-locking mechanisms to
access the shared resources (in this case, stdout).

- --  
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Un ordenador no es un televisor ni un microondas, es una herramienta
Version: GnuPG v1.4.2 (GNU/Linux)


Re: How to send data a little at a time?

Iván Sánchez Ortega wrote:
Quoted text here. Click to load it

Probably easier to just poll a bunch of non-blocking streams.

Site Timeline