2

I am trying to send php variables to a function, but none of the following are passing the variables.

$vars='email=' . $_POST['email'] . '&password=' . $_POST['password'] . '&server=' . $_POST['server'];
$function = 'nohup php unsubscribefunction.php ' . escapeshellarg($vars);
exec($function); 
exec('nohup php unsubscribefunction.php ' . $vars);
exec('nohup php unsubscribefunction.php $vars ');


$vars='?email=' . $_POST['email'] . '&password=' . $_POST['password'] . '&server=' . $_POST['server'];
$function = 'nohup php unsubscribefunction.php' . $vars;
exec($function); 

Here's what the php.net page says about this

When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

However, there are no examples on how to pass the variables, any insights or examples would be appreciated.

2
  • Is there a reason why you have to call another PHP script to the background from the PHP script you're already using? Commented Apr 26, 2011 at 22:29
  • Yes, long story, I'm building this for the 50%+ of my users who don't have javascript enabled. I need to do a page refresh for users who don't have javascript enabled. So without javascript, I need to call the function in the background using exec and then refresh to a page that tells users their search has successfully started. (I love javascript...) Commented Apr 26, 2011 at 22:30

2 Answers 2

2

Try reading through some of the help here. Basically, you will need to make sure you are consuming the command line vars through $argc/getopt() or some other fashion.

Call the script with flags, like so: php myscript.php -a foo -b bar -c baz Test print your arguments:

   <?php
           $arguments = getopt("a:b:c:");
           print_r($arguments);
    ?>
Array
(
    [a] => foo
    [b] => bar
    [c] => baz
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - I was able to get this work - only issue is that it's not performing in the background when I use "nohup php" before the commands - any insight in what i'd need to change so this would run in the background and allow the next page to load?
$vars = escapeshellcmd($_POST['email']) . " " . escapeshellcmd($_POST['password']) . " " . escapeshellcmd($_POST['server']); exec("nohup php unsubscribefunction.php " . $vars); header('Location: location.php');
Try something along these lines: exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile)); where $cmd is your command AND arguments as one variable, $outputfile is a file to store output from your second script and $pidfile is a file to store the process id of the script you're kicking off. This of course, is assuming you are on a Linux/Unix based machine.
1

That's not the problem you're having. You can't send variables this way; you need to use command line arguments to capture the values you want to put in variables.

1 Comment

What would be the call in that case? and how would I use nohup to run it in the background?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.