3

I am working on a simple python web server for educational purposes. I want my server to be able to run PHP scripts on requests. I am able to execute php codes by creating a subprocess like in this answer and get its output as bytes. This can help me to execute the most of the PHP built-in functions. But I would like also to simulate the $_GET and $_POST variables, like, when the user send a POST request, I would like its parameters to be available in $_POST global variable.

I may hack the the command I give to the subprocess, but I heard that, in a real web server like Apache, PHP gets the value of these variables from the environment variables (Common Gateway Interface standard, I think).

However, when I set an environment variable called QUERY_STRING php doesn't set the value of $_GET. Let me show the big picture:

index.php

<?php var_dump($_GET); ?>

my commands in shell:

export QUERY_STRING="foo=bar"
php index.php

This command snipped doesn't give me the output I want (Array => ['foo'] = 'bar'). Here is my questions:

  • What is my mistake in the above code snipped ?
  • How can I inject these two global variables ($_GET and $_POST) to a PHP scripts in python?

My python version is 3.4.3 and I use ubuntu 14.04

2
  • 1
    See this answer Commented Mar 14, 2016 at 14:16
  • 2
    You either write a PHP script targeted at execution on the command line, or targeted at running on a web server. Don't do a weird mixture of both. If it's a CLI script, pass arguments via arguments or stdin, not simulated web requests. Commented Mar 14, 2016 at 14:18

1 Answer 1

1

Try this:

$ php -f index.php foo=bar

And in index php do:

parse_str(implode('&', array_slice($argv, 1)), $_GET);

After that your will get this rezult:

echo $_GET['foo'];// prints 'bar'

Read more in example in the end of the page

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.