Jump to content

Recommended Posts

Does anyone know of a way to execute a function and then not have the browser wait for the return of the function?

 

My intention is for a user to click a button and that will execute the function, then while the function is running, return to the user and let them continue using the app.

 

I did google it and came across popen and ob_flush, not sure what is what and which to use for what i'm trying to do. Any insight is appreciated. Sorry I don't have any code to post.

 

 

There are a few ways that depend on server configuration, so unless you have full control of your web server you should use the most reliable and most portable technique:

 

Make a command-line script to do what you want. If it needs arguments then they should be passed as command-line arguments (which you can get with $argc and $argv). Then in the original script you call it like

`php -f /path/to/script.php -- other arguments you need to pass to it &`;

The trailing & is what makes the script run in the background.

 

Other solutions include:

- Ending the response so everybody else thinks the script is done. I've had mixed luck getting that to work consistently.

- Using process-level control (like popen) to run PHP, like the command does above. Sometimes these functions are disabled, and most commonly on shared hosting servers.

- Forking a process, but this almost never works due to extensions and functions being disabled, or the server (eg, Apache) not supporting the kind of operations forking entails.

Thanks, I appreciate your insight. I do have complete control of the server, so I'm trying anything that works :) And sorry, forgot to mention that I am running PHP on Windows Server 2008.

 

I did this, simple, from the manual, but my question is how do you pass a variable to it?

 


set_time_limit(0); 
$output = shell_exec('test.bat');
echo "<pre>$output</pre>";

 

would it be like this?


set_time_limit(0); 
$var1 = "123456";
$output = shell_exec('test.bat -- $var1 &);
echo "<pre>$output</pre>";


 

 

On Windows you'd use start, like

shell_exec('start test.bat');

 

But are you using batch files? Not PHP?

 

In PHP you'd look at $argv as in

if ($argc >= 2) { // script name always comes first, so $argc=2 means 1 argument
    echo "argv[1] = {$argv[1]}\n";
}

well i'm able to execute the bat file which executes the php script.

 

The PHP script has a function that needs two variables passed to it, i'm wondering how to do that.

 

I even tried

 

$output = shell_exec('test.bat -- ".$argv1." &');

 

So I'm using PHP to execute a bat file that executes a PHP script.

Maybe I'm missing it in the bat file? Here are the three files

 

test.php

 


<?php
set_time_limit(0); 
$argv1 = "123456";
$output = shell_exec('test.bat -- {$argv1[]} &');
echo "<pre>$output</pre>";

?>

 

test.bat

 


"C:\Program Files (x86)\PHP\php-cgi.exe" -f C:\inetpub\test\bkupdate.php

 

bkupdate.php

 


<?php
set_time_limit(0); 

if ($argc >= 2) { // script name always comes first, so $argc=2 means 1 argument
    echo "argv[1] = {$argv[1]}\n";}
echo "Argv1: ".$argv1."<br>";

//rest of the script

?>

1. Windows's CLI doesn't support & for running in the background. That's a Linux thing. Use `start` like in the example I posted.

2. The -- is for php.exe so it doesn't think that whatever comes after is an argument for itself.

3. Your batch file has to pass its arguments to the script.

4. Variables don't work inside singly-quoted strings.

5. bkupdate.php will not get the $argv1 variable. You can only use the $argv array. Like in the example I posted.

 

shell_exec('start test.bat ' . escapeshellarg($argv1));

@echo off
"C:\Program Files (x86)\PHP\php.exe" -f C:\inetpub\test\bkupdate.php -- %1

ok, thanks ,I got it to work. Appreciate it.

 

So my whole purpose was to make a seperate php file so it could execute and then i could tell the browser to move on and not wait for the return, but it still waits for the return. In the php file I put the following that I got from this site:

 

http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/

 

any ideas why it still waits for the page to complete?

 

<?php

   //Redirect to index.php
    header("Location: index.php");
    //Erase the output buffer
    ob_end_clean();
    //Tell the browser that the connection's closed
    header("Connection: close");

    //Ignore the user's abort (which we caused with the redirect).
    ignore_user_abort(true);

    //Extend time limit to 30 minutes
    set_time_limit(1800);
    //Extend memory limit to 10MB
    ini_set("memory_limit","10M");
    //Start output buffering again
    ob_start();

    //Tell the browser we're serious... there's really
    //nothing else to receive from this page.
    header("Content-Length: 0");

    //Send the output buffer and turn output buffering off.
    ob_end_flush();
    //Yes... flush again.
    flush();

    //Close the session.
    session_write_close();

//the rest of the script

?>


 

 

Well, i started a new approach. I'm using the PSTools psexec tool and it works, it executes the script and the app doesn't wait.

 

But I can't figure out how to pass arguments to it.

 

I've tried

 

exec('C:\PSTools\psexec -d "C:\Program Files (x86)\PHP\php-cgi.exe" C:\inetpub\wwwroot\bkupdate.php "$var1" "$var2" ' );

 

exec('C:\PSTools\psexec -d "C:\Program Files (x86)\PHP\php-cgi.exe" C:\inetpub\wwwroot\bkupdate.php <$var1 $var2> ' );

 

and

 

exec('C:\PSTools\psexec -d "C:\Program Files (x86)\PHP\php-cgi.exe" C:\inetpub\wwwroot\bkupdate.php [$var1 $var2] ' );

 

Any ideas? really am thankful for your help. Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.