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.

2
  • 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. Commented Dec 14, 2011 at 1:31
  • Okay thanks. I'm sorry I've never ran PHP on the command line. Commented Dec 14, 2011 at 9:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 for details.
Yeah for sure.. I was assuming he was setting the value himself like $param = "idx" for example
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. :)
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.