开发者

php interpreter and superglobals

开发者 https://www.devze.com 2023-02-24 07:13 出处:网络
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

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.

H开发者_JAVA技巧owever, 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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号