I am trying to create a webserver that serves PHP scripts. Currently, it works as follows:
- The client requests /index.php?test=value
- The server invokes
php index.php
- The server feeds the HTTP request headers as STDIN to the PHP process
- The server reads the output of
php
from STDOUT and returns it to the client
All of this is working except that the parameters are not being passed to the PHP script because:
var_dump($_GET);
returns:
array(0) { }
How do $_GET
parameters get passed to the PHP binary when it is invoked?
Which PHP binary are you using? The CLI or CGI? I suspect you need a CGI version of the binary for PHP to properly handle accept the environment variables and POST data if you pass that.
The
php-cgi
binary implements the CGI interface, which allows you to pass parameters on the command line:Which end up in
$_GET
:You may want to read up on how CGI works so you can implement that in your web server.
Ref: RFC3875
According to Wikipedia, the answer is to set the
QUERY_STRING
environment variable:or in separate steps:
This works if you use the
php-cgi
command, because this is part of the CGI specification. The ordinaryphp
command ignores this variable, but your script might use it.If you pass php script.php test=asdf
If you are passing it to STDIN, you would need to read STDIN and parse the headers yourself. REQUEST_URI would contain the data you need, and you could pass that.