4

first of all, I'm a French student, so excuse me for my poor English level.

We are currently developing a web server (C++) and I must develop the CGI execution part, more exactly : The PHP CGI part.

When a user ask a .php page on our server, we fork/pipe and call the /usr/bin/php interpreter. For example :

$ /usr/bin/php index.php

Now, we can save the result in a buffer (generated html code of index.php), and I can send this content to the client. It's working for a simple script without any variable.

However, lot of php script use some superglobals like $_GET and $_POST. My problem is : How can I give to the php interpreter this argument ?

Example : How can I set this $_POST variable in the aim to save "Hello world" in our buffer ?

<?php

echo $_POST['text'];

?>

Thank you for your future responses.

cordially

user702041
  • 53
  • 5

2 Answers2

4

CGI programs expect POST data on stdin. and GET data in the QUERY_STRING environment variable.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
3

You have to set some environment variables:

  • REQUEST_METHOD=POST to tell PHP that it needs to handle a POST request
  • CONTENT_LENGTH=1234 to tell PHP how many bytes it will receive as raw POST data (in this case 1234 bytes)
  • HTTP_CONTENT_LENGTH basically the same as CONTENT_LENGTH. Better set this too so that it will work better with the various PHP versions/configurations.
  • CONTENT_TYPE=application/x-www-form-urlencoded is the HTTP Content-Type header

You get the right values for these variables from the HTTP header.

You also need a bidirectional pipe to the PHP process to send the raw POST data to it's STDIN. I assume you are already receiving the script output from PHP.

As long as you handle a normal browser request, you don't need to know any more details. Otherwise, if the POST data comes directly from your server, use the CONTENT_TYPE above and url-encode the variables:

  • REQUEST_METHOD=POST
  • CONTENT_LENGTH=16
  • HTTP_CONTENT_LENGTH=16
  • CONTENT_TYPE=application/x-www-form-urlencoded

STDIN data: test=Hello+world

For GET requests you change REQUEST_METHOD=GET and leave away the other variables. In either case you can pass the query string via the QUERY_STRING environment variable.

Udo G
  • 12,572
  • 13
  • 56
  • 89
  • Thank you so much for your responses. I tried to respect your advices but ... POST requests not works for me :( I use 2 binary. The first one load the environment variables : http://pastebin.com/XbaaxRWF The other one call the php interpreter : http://pastebin.com/XYLv1yjV Between this 2 binary, we have à pipe in the aim to tranfert all informations. Have you got an idea ? The GET method works, so thanks to you =D Have a nice day. – user702041 Apr 13 '11 at 11:05
  • I had no time to have a look at your source code, but instead of running PHP try to start a shell script (or similar) that writes the environment variables and STDIN data to a text file and check that data. You could also just run `set` and `cat` alone and should get the info back on STDOUT. That way you can be sure that it's not PHP that refuses to work because one of it's security restrictions... – Udo G Apr 15 '11 at 10:03