1

I need to pass some $usertoken value to shell_exec function inorder to execute another PHP file with this value from my server.

Here is my code :

<?php
$usertoken= array( $_GET['usertoken'] );
$findme   = 'valid_device';
$pos = strpos($usertoken, $findme);

if ($pos === false) {
    echo "The string '$findme' was not found in the string '$usertoken'";
} else {
    echo " Device is valid ### $usertoken Continue to push_script  ### ";
    echo shell_exec('cd /mypath; /usr/local/bin/php -q push_script.php -q "{$usertoken}"');
    }
?>

The push_script.php is waiting with $_GET to the $usertoken value

How can I pass this argument to the PHP ?

I understood from what I was reading that I should use maybe some parse_str() with combination of $argv in order to parse the $usertoken to the PHP script but I can't figure it out how to do it.

I will appreciate any help

Abraham
  • 43
  • 1
  • 7
  • possible duplicate of [Shell run/execute php script with parameters](http://stackoverflow.com/questions/6763997/shell-run-execute-php-script-with-parameters) – kamal pal Jul 26 '15 at 18:27

2 Answers2

2

Variables aren't expanded inside single-quoted strings, only inside double-quoted strings, so swap your quotes.

echo shell_exec("cd /mypath; /usr/local/bin/php -q push_script.php -q '{$usertoken}'");

What is the difference between single-quoted and double-quoted strings in PHP?

Also, $usertoken should just be a string, not an array, so the assignment should be:

$usertoken = $_GET['usertoken'];
Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks Barmar, I changed to single-quoted but it still doesn't work. The value is passed to the PHP script but then the script output a reslut error that mean the value is incorrect. Maybe the problem is with the shell_exec syntax ? – Abraham Jul 26 '15 at 12:50
  • Does it work if you run `php -q push_script.php -q 'token'` from the command line? – Barmar Jul 26 '15 at 12:52
  • `shell_exec` looks fine. Are you sure you need the extra `-q` after `push_script.php`? That argument gets sent to the script. – Barmar Jul 26 '15 at 12:52
  • Running the `php push_script.php 'token_number'` from the command line is working fine but still the same problem - the PHP result with error which means that from some reason the string value didn't pass correctly. And yes I don't need the -q – Abraham Jul 26 '15 at 14:46
  • Well ignore my last comment I thought you want me to run the frist php script. Well indeed it is very strange - running the `php push_script.php 'token'` from the command line result with the error output. BTW, when running the push_script via the web with php?usertoken=XXX is working fine. So I don't understand what could be the problem ? Maybe some server configuration ? – Abraham Jul 26 '15 at 15:03
  • I found what was the problem. It appears that when you trying to run PHP CLI in some server environment you should use the following path : `/usr/local/php54/bin/php-cli` that probably change from each server. Now I can run the script with parameter. Thanks for you help – Abraham Jul 27 '15 at 08:34
0

You will get your value in reserved variable $argv

In push_script.php $argv[1] will get you desired parameter passed, Also swap your quotes as @Barmar indicated.

kamal pal
  • 4,187
  • 5
  • 25
  • 40
  • Hi Kamal thank for the suggestion but I didn't understand how do I use the $argv . Do I need to replace it in the `push script.php` instead of the `$_get()` or is it in addition ? Should I also add something in the execution script ? – Abraham Jul 26 '15 at 17:11
  • @Abraham you need to replace `$_GET['usertoken']` with `$argv[1]` .. as far as I know `$_GET` would be served under `http / https` (may be more, which I am not aware of ), I had already posted link to PHP Manual for ref in my answer. here again http://php.net/manual/en/reserved.variables.argv.php See this post too.. http://stackoverflow.com/questions/6763997/shell-run-execute-php-script-with-parameters – kamal pal Jul 26 '15 at 18:24
  • @Abraham you can `print_r($argv)`, and see all your params where 0 index will be the file which you are executing .. or if you like to use `$_GET` .. you can execute you script via curl or wget.. like `curl http://yourdomain.com/script_to_be_executed.php' – kamal pal Jul 26 '15 at 18:26
  • thanks It works ! and it's indeed goo solution when you want to combine https request and passing on the parameter to PHP CLI script – Abraham Jul 27 '15 at 08:36