16

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?

2
  • Which web server? How have you configured the web server? How have you configured PHP? Commented Oct 3, 2010 at 7:00
  • @jos: It's JetHTTP - one I wrote myself. Commented Oct 3, 2010 at 7:28

4 Answers 4

32

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:

php-cgi -f index.php left=1058 right=1067 class=A language=English

Which end up in $_GET:

Array
(
    [left] => 1058
    [right] => 1067
    [class] => A
    [language] => English
)

You may want to read up on how CGI works so you can implement that in your web server.

Ref: RFC3875

1
  • Yup... sure enough. That was the problem. Commented Oct 4, 2010 at 23:34
3

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

QUERY_STRING='user=foo&pass=bar&left=1234' php-cgi index.php

or in separate steps:

export QUERY_STRING='user=foo&pass=bar&left=1234'
php-cgi index.php

This works if you use the php-cgi command, because this is part of the CGI specification. The ordinary php command ignores this variable, but your script might use it.

0
1

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.

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

A complete, yet simple solution to have parameters through $_GET, whether requested from CLI of any web servers or from URL:

if (empty($_GET)) {
  if (isset($argv))
    parse_str(implode('&', $argv), $_GET);
  elseif (isset($_SERVER['argv']))
    parse_str(implode('&', $_SERVER['argv']), $_GET);
}

Therefore both below requests give us foo GET parameter:

http://example.com/test.php?foo=bar

/bin/env php /.../test.php foo=bar

2
  • The question is how to provide the variables, not how to use them. Commented Dec 19, 2024 at 11:09
  • @GeraldSchneider Thanks for your comment alongside with downvote but the solutions provided by other answers don't work in all cases and some servers don't turn args into $_GET, therefore it's needed to manually turn them into $_GET in the script. Commented Dec 19, 2024 at 16:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.