Asynchronous Processing With PHP

Several weeks ago I was looking on line for a way to have PHP execute a background process, so that the page didn’t hang while some functionality happened. One way to do that is to use fsockopen() to open a url to exec what you want. But I wanted to see if there was another way. This is what I came up with.

I should note that this isn’t that well tested. I ran it on a Windows and Linux system without incident to test it, but I haven’t given it a very thorough exam. Also note $call is completely unsanitized. If you’re using this and accepting anything from the web, you need to make really sure that $call is completely sane and free of evil.

< ?php
/* Note that the call itself isn’t sanitized in any way, so it’s
   important to make sure that this can’t be exploited, see
   docs for escapeshellcmd() for details
*/
 
// Demonstration
if(launchBackgroundProcess(‘touch testfile.txt’)){
    print ‘Successfully launched background process’;
}
 
 
 
/**
* Launch Background Process
*
* Launches a background process (note, provides no security itself, $call must be sanitized prior to use)
* @param string $call the system call to make
* @author raccettura
*/
function launchBackgroundProcess($call) {
 
    // Windows
    if(is_windows()){
        pclose(popen(‘start /b ‘.$call., ‘r’));
    }
 
    // Some sort of UNIX
    else {
        pclose(popen($call.‘ /dev/null &’, ‘r’));
    }
    return true;
}
 
 
/**
* Is Windows
*
* Tells if we are running on Windows Platform
* @author raccettura
*/
function is_windows(){
    if(PHP_OS == ‘WINNT’ || PHP_OS == ‘WIN32’){
        return true;
    }
    return false;
}
?>

To run a php process you can obviously use launchBackgroundProcess('php /path/to/script.php');.

Now say we wanted to load a page while retrieving data from a process to display inline. We don’t want to wait for the entire command to finish, since that could take a while, and the user may believe something is wrong and reload (slowing down the system even more). A good example of this is an online traceroute utility. Here’s a quick example of that:

< ?php
    /****
     * Again notice this is unsanitized since we trust ourselves.  Coming from the web
     * it would need to be sanitized to ensure it’s safe to use.  see escapeshellarg()
     ****/
    $hostname = ‘accettura.com’;
?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Traceroute to $hostname</title>
</head>
<body>
< ?php
    if(is_windows()){
        $cmd = ‘tracert -w 10’;    
    } else {
        $cmd = ‘traceroute -w 10’;
    }
    $handle = popen("$cmd $hostname 2>&1", ‘r’);
 
    while(!feof($handle)) {
        $buffer = fgets($handle);
        $buffer = ‘<p>’.trim(htmlspecialchars($buffer)).‘</p>’;
 
        echo $buffer;
        ob_flush();
        flush();
    }
    pclose($handle);
 
 
 
   /**
    * Is Windows
    *
    * Tells if we are running on Windows Platform
    * @author raccettura
    */
    function is_windows(){
        if(PHP_OS == ‘WINNT’ || PHP_OS == ‘WIN32’){
            return true;
        }
        return false;
    }
?>
</body>
</html>

Isn’t that simple?

There’s more than 1 way to approach a problem, and there may be better ways out there. I won’t be so cocky as to think these are by any means perfect. But they may be of help to someone who also notices the lack of documentation on little (but important) things like this.

Just remember: always sanitize your input when working with stuff like this, or your putting your system at risk.

Tags: , ,


22 Responses to “Asynchronous Processing With PHP”

  1. Very nice!

    It would be cooler if you could pass a callback function name.

  2. sean says:

    This looks good; I am trying to figure out a way to get a PHP script to run in the background on a Windows server.

    However, I can’t figure out your use of quotes, they seem to be uneven.

    What am I missing?

  3. Robert says:

    Sean: you can use both ' or " in PHP as long as you use them as “This “.’that’. Single quotes won’t be evaluated. For example:

    $test = ‘value’;
     
    print "This is a $test"; // will be: This is a value
    print ‘This is a $test’; // will be: This is a $test
  4. […] a background process in an OS-independent fashion you’d can use the function below (snatched from this post) […]

  5. johny why says:

    this ain’t workin. any ideas? running on a linux host:

    if this is shelling out to the system, then i don’t have shell access. is there way way to do an asynchronous call to a php script WITHOUT calling the shell?

    thanks

  6. johny why says:

    the code:

    pclose(popen(‘PHP http://picnictoimpeach.us/remote.php5‘, ‘r’));

  7. diiar says:

    This doesn’t work with me. I don’t know what’s wrong, but lauchBackgroundProcess() doesn’t do anything. However, if I add fgets() as in the second script, it works. But, it’s not an asynchronous call!!

  8. Robert says:

    @diiar: It’s possible your host prohibits the use of popen() for security reasons. You should check with your admin.

  9. Pham Loc says:

    Thanks man you saved my day

  10. Bill says:

    I think the thing that was confusing ppl is the use of quotations in the following line:

    pclose(popen(‘start /b ‘.$call.”, ‘r’));

    This should actually be:

    pclose(popen(‘start /b ‘.$call, ‘r’));

    This also saved my day. Thanks man!

  11. hidden says:

    I don’t see any asynchronous code.
    fgets ist waiting for input line.
    All you do is flushing php buffer (ob_flush(); flush()). There is no sense doing this in a pipe.
    Do something else instead of a traceroute(6) without response line by line and your code will wait unlimited.

  12. to says:

    thanx. Save my day

  13. […] a background process in an OS-independent fashion you’d can use the function below (snatched from this post) […]

  14. Ölbaum says:

    That WordPress moron replaced all apostrophes with curly quotes in your code!!!

  15. SQC says:

    Where’s the callback?

  16. […] process in an OS-independent fashion you’d can use the function below (snatched from this post) […]

  17. […] an asynchronous process with it other than using exec or popen. There is a blog post about that here. Your idea for a queue in MySQL is a good idea as […]

  18. zeet says:

    How to call a php class method using this ? Is it possible at the first place?

  19. zeet says:

    How to call a php class method using this ? Is it possible in the first place?

  20. zeet says:

    How to call a php class method using launchBackground ? Is it even possible in the first place?

  21. […] an asynchronous process with it other than using exec or popen. There is a blog post about that here. Your idea for a queue in MySQL is a good idea as […]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.