1

I am trying to send values from POST variable to Python 2.7 using shell_exec function of PHP. When I try to use the pre-defined variables, it gives the correct results, and when I use POST variables, it does not send parameters to the python file. and also the python script doesn't execute.

I am using AJAX method to call this file

    //Does not work in this case
         $age = (int)$_POST['age'];
          $gander = $_POST['gander'];
          $occupation = $_POST['occupation'];
          $n = 5;

    //Works when used like this
        $age=25;
        $gander='M';
        $occupation='engineer';
        $n=5;

//For result displaying
          $res = shell_exec("py -2 main.py $age $gander $occupation $n ");
          echo $res;
stackuser
  • 168
  • 2
  • 6
  • 15
  • Are you aware how incredible insecure what you are trying to do is, right? – yivi Nov 24 '18 at 08:57
  • I don't know quite how to phrase this, but I would fire anybody who put that code on the web for gross negligence. Passing POST parameters to a shell script directly opens up a massive can of exploits. That said, [two methods for including variables in strings](https://stackoverflow.com/q/4676417/96588). – l0b0 Nov 24 '18 at 11:41
  • Hi, Thanks for your response, How should then I pass the parameters to the python file from Ajax request? This was the only way I found on google, – stackuser Nov 25 '18 at 02:14

1 Answers1

1

Both variables are almost similar. As you are using ajax method, there may be a possibility that your post values have different format or length, so You can use var_dump($var) to check that and correct the difference between both sets of variables.

asad makhdoom
  • 442
  • 4
  • 3