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?

0

4 Answers 4

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

9
  • 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. Oct 6, 2012 at 3: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 6, 2012 at 4:27
  • so I just do $param1 = "My Value Here" Oct 6, 2012 at 4: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 6, 2012 at 4: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. Oct 6, 2012 at 4:54
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?

2
  • 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. Oct 6, 2012 at 3: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. Oct 6, 2012 at 4: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.

-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(); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.