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

Community
  • 1
  • 1
Bora
  • 1,778
  • 4
  • 17
  • 28
  • 1
    See this [answer](http://stackoverflow.com/questions/4186392/php-passing-get-in-linux-command-prompt) – frz3993 Mar 14 '16 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. – deceze Mar 14 '16 at 14:18

1 Answers1

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