Support » Plugins and Hacks » how to handle long process?

  • I am planning on a plug-in that will require a significant amount of processing after the user POST the form. I would like to be able to redirect the user to another page while the process is still running. Here’s what I had in mind, but the header redirection wouldn’t start until the whole script is completed. Any idea? I thought of using exec() to spawn another script, but there would pose some portability issue (unix vs. win32).

    <?php
    ignore_user_abort(TRUE);
    header(“Location: http://something.com/somepage.html“);
    ob_flush();
    flush();

    // the following is to simulate some long task that will happen…

    $handle = fopen (dirname(__FILE__) . ‘/data.txt’, “w”);
    if ($handle) {
    for ($i = 0; $i < 10; $i++) {
    // echo date(“Y-M-d G:i:s”) . ” Try number ” . $i . “\r\n”;
    fwrite($handle, date(“Y-M-d G:i:s”) . ” Try number ” . $i . “<br>\r\n”);
    sleep(2);
    }
    fwrite($handle, “Finally done!!!\r\n”);
    }
    fclose($handle);

    ?>

  • The topic ‘how to handle long process?’ is closed to new replies.