0

I'm having a problem on passing the parameters from php to python.

By using the $_SERVER['QUERY_STRING'].

http://www.domain.com/path?a_num=123&msg=hello

i will put the a_num=123&msg=hello to a variable

$a = $_SERVER['QUERY_STRING'];
system("python python.py ".$a);

and in python will print it

a = sys.argv[1]
print a

and the result is *a_num=123* only

what is the problem?

2
  • 1
    The & is cutting off the rest of the string, but I'm not familiar enough with php to know how to rectify that. Commented Jan 30, 2014 at 1:51
  • 1
    @jmu303 yeah. i think so too. Commented Jan 30, 2014 at 1:53

1 Answer 1

1

I don't think this is a problem with PHP, more how the system command is being executed. Assuming you are using Linux, the '&' character in the command:

python python.py a_num=123&msg=hello

Is being interpreted as a control operator. From the documentation for bash (although this applies equally to other shells such as tcsh):

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

To prevent this, you need to quote the string being passed:

python python.py "a_num=123&msg=hello"

Which in PHP would look like:

system("python python.py \"".$a."\"");
Sign up to request clarification or add additional context in comments.

Comments

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.