I am trying to create a webserver that serves PHP scripts. Currently, it works as follows:

  1. The client requests /index.php?test=value
  2. The server invokes php index.php
  3. The server feeds the HTTP request headers as STDIN to the PHP process
  4. 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?

link|improve this question

79% accept rate
Which web server? How have you configured the web server? How have you configured PHP? – joschi Oct 3 '10 at 7:00
@jos: It's JetHTTP - one I wrote myself. – George Edison Oct 3 '10 at 7:28
feedback

4 Answers

up vote 1 down vote accepted

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. You may want to read up on how CGI works so you can implement that in your web server.

Ref: RFC3875

link|improve this answer
Yup... sure enough. That was the problem. – George Edison Oct 4 '10 at 23:34
feedback

According to Wikipedia, the answer is to set the following environment variable:

QUERY_STRING

Then the PHP binary will pick this up and use it.


Update: Actually, I just tried this and it doesn't work.

link|improve this answer
feedback

If you pass php script.php test=asdf

$result = parse_args($argv,$argc,$help);
print_r($result);

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.

link|improve this answer
I need the variables to be available in $_GET like they are when PHP is used under Apache. – George Edison Oct 3 '10 at 6:20
All well and good until someone visits /index.php?test=asdf;rm%20-rf%20/ – danlefree Oct 3 '10 at 6:54
@dan: Don't worry, my webserver is smart enough to handle that. – George Edison Oct 3 '10 at 7:30
feedback

I tried both php and php-cgi and they seem to work for the following:

REQUEST_METHOD="GET" QUERY_STRING="test=blah"

This populates $_SERVER['QUERY_STRING'] but not $_GET['test']

Anyone know why? Is there some PHP setting I need to set?

link|improve this answer
Are you populating REQUEST_URI with the full request URL and query string? – George Edison Jan 8 '11 at 1:43
there were two places where I needed to set php-cgi and I only set one. OK here's what I learned: Without setting cgi.force_redirect = 0 in php.ini I have to set the environment variable: REDIRECT_STATUS="200" Not sure how to get around that. Any ideas? Then when I ran it, PHP returned headers and stuff but it said my test.php file is "not found". I fixed this by setting the environment variable: PATH_TRANSLATED to the full path for test.php I also need to figure out how to get rid of the headers PHP returns: X-Powered-By: PHP/5.3.3-1ubuntu9.1 Content-type: text/html – I need help Jan 8 '11 at 4:40
php-cgi -q seems to get rid of the headers, is that an ok way to do it or am i missing something – I need help Jan 8 '11 at 4:52
The headers provided are supposed to be included with the response headers you send to the client. The response that php-cgi returns consists of one or more headers (separated by CRLF), then two CRLFs, and then the content body. – George Edison Jan 9 '11 at 20:39
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.