Php translator and superglobal

First of all, I am a French student, so excuse me for my poor English level.

We are currently developing a web server (C ++), and I have to develop the implementation of the CGI part, more precisely: the PHP CGI part.

When the user requests the .php page on our server, we use fork / pipe and call the / usr / bin / php translator. For instance:

$ / usr / bin / php index.php

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

However, many PHP scripts use some superglobal, such as $ _GET and $ _POST. My problem: how can I pass this argument to the php interpreter?

Example. How can I set this $ _POST variable in order to save "Hello world" to our buffer?

<?php echo $_POST['text']; ?> 

Thanks for your future answers.

Cordially

+4
source share
2 answers

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

+4
source

You must set some environment variables:

  • REQUEST_METHOD=POST tell PHP that it should process the 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 is basically the same as CONTENT_LENGTH . Better set this up so that it works better with different versions / configurations of PHP.
  • CONTENT_TYPE=application/x-www-form-urlencoded - HTTP content header

You get the correct values ​​for these variables from the HTTP header.

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

While you are processing a normal browser request, you no longer need to know any details. Otherwise, if the POST data comes directly from your server, use CONTENT_TYPE above and url-encode 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 other variables. In either case, you can pass the query string using the QUERY_STRING environment QUERY_STRING .

+3
source

Source: https://habr.com/ru/post/1347534/


All Articles