0

Possible Duplicate:
php in background exec() function

After a certain script runs on website I want to do a bit of processing in the background which could take a little long to run on the page request. I heard you can do this by using the exec() method to run a PHP script. If this is a good way to do it how do I pass a query string to PHP script with the exec() method?

EDIT: It's not a duplicate because the post you're referring to doesn't deal with my question about query string/argument passing.

Community
  • 1
  • 1
Undefined
  • 1,899
  • 6
  • 29
  • 38
  • What do you mean by "query string" -- when using PHP on the command line, instead of through apache, there is no _GET array. You deal with the $args array of command-line arguments. These are passed on the command line after the script name itself, with spaces separating them. – Rylab Dec 14 '11 at 01:31
  • Okay thanks. I'm sorry I've never ran PHP on the command line. – Undefined Dec 14 '11 at 09:43

2 Answers2

1

If you want to run a PHP script in the background using PHP, then you can do the following:

$command = "php -d max_execution_time=50 -f myfile.php '".$param."' >/dev/null &";
exec($command);

$param is a variable you want to pass into the file.

George P
  • 736
  • 5
  • 12
  • Using `exec()` in this fashion can be dangerous if `$param` was supplied from (or improperly derived from) user-supplied input. See [Wikipedia's notes on shell injection vulnerabilities](http://en.wikipedia.org/wiki/Shell_injection#Shell_injection) for details. – sarnold Dec 14 '11 at 01:31
  • Yeah for sure.. I was assuming he was setting the value himself like $param = "idx" for example – George P Dec 14 '11 at 01:32
  • Since you took care to use `max_execution_time` I figured _your_ code would be fine -- but not everyone reading this would know that already. :) – sarnold Dec 14 '11 at 01:34
0

Since your script runs in the background and not in a terminal, try ignore_user_abort( TRUE ); execution flow continues until execution is complete, and not when the tab or window or network connection is closed.