sanchez77 Posted June 1, 2012 Share Posted June 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/ Share on other sites More sharing options...
requinix Posted June 1, 2012 Share Posted June 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350363 Share on other sites More sharing options...
sanchez77 Posted June 1, 2012 Author Share Posted June 1, 2012 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350374 Share on other sites More sharing options...
requinix Posted June 1, 2012 Share Posted June 1, 2012 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350378 Share on other sites More sharing options...
sanchez77 Posted June 1, 2012 Author Share Posted June 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350380 Share on other sites More sharing options...
sanchez77 Posted June 1, 2012 Author Share Posted June 1, 2012 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350381 Share on other sites More sharing options...
requinix Posted June 1, 2012 Share Posted June 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350404 Share on other sites More sharing options...
sanchez77 Posted June 2, 2012 Author Share Posted June 2, 2012 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350467 Share on other sites More sharing options...
requinix Posted June 2, 2012 Share Posted June 2, 2012 It probably depends on what "the rest of the script" is. Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350469 Share on other sites More sharing options...
sanchez77 Posted June 2, 2012 Author Share Posted June 2, 2012 the rest of it uses SSH to process commands. my intent is to have that script just execute and run and then have the web page move forward without waiting for the script to finish. Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350470 Share on other sites More sharing options...
requinix Posted June 2, 2012 Share Posted June 2, 2012 Oh, so the code you posted is bkupdate.php. What's the current version of test.php? Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350521 Share on other sites More sharing options...
sanchez77 Posted June 2, 2012 Author Share Posted June 2, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350623 Share on other sites More sharing options...
silkfire Posted June 2, 2012 Share Posted June 2, 2012 Why not use AJAX and JavaScript? I mean the button is on the client side, not server? Quote Link to comment https://forums.phpfreaks.com/topic/263493-execute-function-but-not-make-the-browser-wait-for-a-return/#findComment-1350637 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.