2

I have this user form. The user will submit data. Then with that data, I want to send that data over to a PHP script which will run in the background. Reason being, is because the script has a lot of sleeps, etc, and I don't want to keep the user waiting.

So the basics of my question is: How can I run a PHP script in the background, and how can I pass a parameter (like GET) to it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1687621
  • 425
  • 1
  • 6
  • 14

4 Answers4

0

Something like this would work

http://gearman.org/index.php?id=manual:job_server

But, why do you have so many sleeps in your script?

  • Free users on my site has to wait a minute before getting their results. Instead of sitting there waiting on the page, they can just come back later. I just want to run code in the background that has the 60 second sleep, and then send them to a success page right after they submit. Also, that looks too complex. I was hoping for an answer that just involves shell_exec, but with ability to pass paramaters. – user1687621 Oct 06 '12 at 03:20
  • If you're looking for something simple like that, why don't you update the page to not sleep, and instead deny free user requests that haven't waited long enough, and do the request through ajax. – Johnathon Malizia Oct 06 '12 at 04:01
0

try use AJAX, is a asynchronous javascript and xml, there is a simple example: http://www.w3schools.com/php/php_ajax_intro.asp

in AJAX the page will not refresh and the result are sended by JSON or XML.

Leo
  • 1,990
  • 1
  • 13
  • 20
0

You can use exec to run your background script. Use something like

exec('php -f bg_script.php -- '.escapeshellarg($param1).' '.escapeshellarg($param2).' > /dev/null & ');

In your script bg.php you can get the passed parameters from the array $argv

air4x
  • 5,618
  • 1
  • 23
  • 36
  • I only have one param. So when referincing that param I just do $argv[0]? If you can gve me example how $param1 is set in my first script, i'd appreciate it. – user1687621 Oct 06 '12 at 03:59
  • use `$argv[1]` to get the first argument. `$argv[0]` will contain the name of the called script. Before calling `exec` just set `$param1 = 'some_string' ;`. – air4x Oct 06 '12 at 04:27
  • so I just do $param1 = "My Value Here" – user1687621 Oct 06 '12 at 04:46
  • Yes. You can only pass a string. If you want to pass an array, you would have to serialize it before passing. – air4x Oct 06 '12 at 04:53
  • Okay, everything worked perfectly. Only problem, it still wanted to wait. But the script actually did run. But it still waited on the page, and didn't actually run in the background truely. – user1687621 Oct 06 '12 at 04:54
  • did you include the last redirect ` > /dev/null ` in your command ? also add `&` after it. i.e. ` > /dev/null &` – air4x Oct 06 '12 at 04:55
  • Yes. But I did not add the & after it. How does the & help? EDIT: Also added the &, still wants to wait for the script to end. – user1687621 Oct 06 '12 at 21:23
  • can you show your `exec` code? The `&` given at the end of the command will send the process to the background. With the `&` it should really work. – air4x Oct 06 '12 at 21:43
  • Yea. For some reason it didn't upload when I made the change. I reuploaded and now it is working. Thanks for your help :) – user1687621 Oct 06 '12 at 21:57
-1

What about file_get_contents() with timeout.

if (isset($_GET['async'])) {

    for( $i = 0 ; $i <= 5 ; $i++ )
    {
        append_log(date('l jS \of F Y h:i:s A') . ': background process. parameter ' . $i . ': ' . $_GET[$i] . '<br />');
        sleep(1);
    }

    exit;
}

header( 'Content-type: text/html; charset=utf-8' );
$parameters = array('async' => true, 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
pseudo_async($parameters);  // this runs this php script in the backbround

echo 'Begin ...<br />';
for( $i = 0 ; $i <= 5 ; $i++ )
{
    output_buffer('appended to the log <br />');
    append_log(date('l jS \of F Y h:i:s A') . ': main process.<br />');
    sleep(1);
}
echo 'End ...<br />';


function pseudo_async($query) {

    $timeout = array('http' => array('timeout' => 0.01));
    $context = stream_context_create($timeout);

    @file_get_contents(selfurl() . '?' . http_build_query($query), false, $context);
}

function append_log($msg) {
    $file = __DIR__ . '/log.html';
    file_put_contents($file, $msg, FILE_APPEND);
}

function selfurl() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80")
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    else 
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    return $pageURL;
}

function output_buffer($str) {
    echo $str;
    flush();
    ob_flush(); 
}
Teno
  • 2,582
  • 4
  • 35
  • 57