Web Hosting Talk







View Full Version : [PHP] Realtime output with shell_exec ?


houkouonchi
01-30-2010, 11:29 AM
Warning !! PHP noob!!

I wanted to make a script page/script for running ping, traceroute, etc... but instead of showing the complete output after the command is running I want it to show the current output it is getting while the command is running (useful in traceroutes which if they start timing out at the last hop can take quite a bit of time to complete!

I know you can do this using ob_ functions for example:


ob_start();
for($i=0; $i < 10; $i++)
{
echo $i, ' ';
ob_flush();
flush();
sleep(1);
}


Which will print 1-10 waiting one second each time. This works fine on my server but I am not sure how to implement this with shell_exec. Can anyone lend me some expertise?

I know a couple traceroute/ping scripts exist out there but most of the ones I have tried suck and don't even escape the input from the browser and thus is basically giving someone shell access to my server via the user that apache runs as so I was thinking of just writing my own simple code to do what I want.

csparks
01-30-2010, 11:56 AM
The problem is the the exec() function does not return anything until the command is complete.

As far as I am aware, there is no function that will return realtime results from a command line call.

luki
01-30-2010, 02:01 PM
The passthru() function might work...

shackrat
01-30-2010, 04:18 PM
Look at the popen command it should do what you are seeking.