8

Update:

In a fit of desperation, I did the following in a shell:

REDIRECT_STATUS=true
SCRIPT_FILENAME=/var/www/...
REQUEST_METHOD=POST
GATEWAY_INTERFACE=CGI/1.1
export REDIRECT_STATUS
export SCRIPT_FILENAME
export REQUEST_METHOD
export GATEWAY_INTERFACE
echo "test=1" | php-cgi

...and STILL no $_POST variables are showing up in the output of this:

<?php var_dump($_POST); ?>

I am trying to create a small webserver that interfaces with the php-cgi binary. However, things aren't going so well. The php-cgi binary correctly handles GET requests. When it comes to POST requests, the $_POST array is empty, even when things are getting POSTed.

I've checked the HTTP headers being fed into the php-cgi binary and they do indeed include the POST data and the Content-type: application/x-www-form-urlencoded header.

What could be keeping the php-cgi binary from seeing that there's POST data included in the request?


I'm making progress, I've dug up some stuff from the PHP source code:

  • /sapi/cgi/cgi_main.c:

    468: static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)

(I have no idea where this function is invoked from.)


After reading the answer below, I tried:

<?php

var_dump($HTTP_RAW_POST_DATA);

?>

...which yielded the output:

NULL

...indicating that something even stranger is at work here.


I'm getting closer... I found this function in /main/php_content_types.c:

SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader)

...and it seems to be the code that processes POST requests.

4
  • 1
    Time to pick through the PHP source :P Oct 27, 2010 at 5:17
  • 1
    I don't have an answer, but POST data should be coming in via stdin. Oct 27, 2010 at 5:18
  • Does your web server implement the CGI 1.1 specification correctly?
    – joschi
    Oct 27, 2010 at 6:22
  • @joschi: I believe so... plus it works fine for GET requests, so I'm guessing that it's something else. Oct 27, 2010 at 6:37

3 Answers 3

8

I finally figured it out:

Apparently the CONTENT_LENGTH environment variable needs to be set.

Adding:

CONTENT_LENGTH=6
export CONTENT_LENGTH

to my example above causes it to work properly!

2
  • You should also probably set Content-type at a minimum - basically, all that HTTP stuff you were trying to pipe into stdin in your previous version of the shell script needs to be set as an environment variable before the CGI is started. Oh, and you should mark this answer as "accepted" :)
    – TML
    Oct 27, 2010 at 22:31
  • @TML: True - I have added 'Content-type' now that it's working. I can't accept this until 24 hours after I post it. (IIRC) Oct 28, 2010 at 5:20
4

You mentioned that your system did GETs just fine. content length only needs to be set for POSTs

Also this might help, it's the CGI/1.1 specification. It shows what environment variables need to be set:

http://graphcomp.com/info/specs/cgi11.html and this lib.ru/WEBMASTER/cgi1_1spec/interface.html

What were the minimum environment variables you needed to get GET requests working?

3
  • SCRIPT_FILENAME, REQUEST_METHOD, and REDIRECT_STATUS. Jan 7, 2011 at 17:50
  • REDIRECT_STATUS isn't required if you have cgi.force_redirect = 0 in your php.ini. I tried adding it and SCRIPT_FILENAME and it still wont work Jan 7, 2011 at 18:37
  • CONTENT_TYPE for me, PHP 7.0.16
    – Phil
    Mar 25, 2018 at 9:25
0

Try checking the $HTTP_RAW_POST_DATA variable.

http://php.net/manual/en/reserved.variables.httprawpostdata.php

2
  • It's coming up as NULL. Why is it doing that? Oct 27, 2010 at 5:59
  • Because $HTTP_RAW_POST_DATA is a terrible solution - file_get_contents('php://input') is better, but will not work with multipart/form-data encoded data.
    – TML
    Oct 27, 2010 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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