简体   繁体   中英

PHP development server: $HTTP_RAW_POST_DATA is not populating $_POST… how come?

I'm a little stumped on the following, any help appreciated. I've removed error checking code for this example of what's going wrong...

I'm running PHP 5.4.14 and using the PHP server...

EDIT: Thanks to help from Álvaro I can now see that the issue is that the $HTTP_RAW_POST_DATA does actually hit the PHP development server, but for some reason it is not used to populate $_POST .

The snippet of JavaScript (taken from real code and simplfied here) that's called when I click the form submit button is...

xmlhttp = new XMLHttpRequest();

<snip>
params = 
    "forename=" + encodeURIComponent(form.forename.value) + "&" + 
    "surname="  + encodeURIComponent(form.surname.value)  + "&" +   
    "nonce="    + encodeURIComponent(form.nonce.value);


<snip>
xmlhttp.open("POST", url, false);

<snip>
if (xmlhttp.overrideMimeType) 
{
    xmlhttp.overrideMimeType('text/html');
}
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");

alert(params);
xmlhttp.send(params);

From the alert I can see that the params are okay.

The POST request then correctly hits my script, but an error log of the $_REQUEST array (and also $_POST ) gives me an empty array. The error log of the $_REQUEST/_POST is done as soon as I enter the script.

My script replies and the javascript can pick up the reply... but of course all my script can return is an error code... doh.

Any ideas why this might be the case?

EDIT: Thanks to Álvaro G. Vicario for his help so far. Now I can see that the request is indeed leaving the browser OK.

Request URL:http://localhost:8000/php_database/search_member.php
Request Method:POST
Status Code:200 OK
Request Headers
    POST /php_database/search_member.php HTTP/1.1
    Host: localhost:8000
    Connection: keep-alive
    Content-Length: 69
    Origin: http://localhost:8000
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
    Content-type: application/x-www-form-urlencoded
    Accept: */*
    Referer: http://localhost:8000/php_database/db_search.php
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: en-US,en;q=0.8
    Cookie: PHPSESSID=954ebbfadb841ef659a3961a44d715871bdedbaa
Form Data
    forename=ss&surname=dd&nonce=6c3e75f500dffdbfefe95d91710432dd8fd23fab
Response Headers
    HTTP/1.1 200 OK
    Host: localhost:8000
    Connection: close
    X-Powered-By: PHP/5.4.14
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Content-type: text/xml

The PHP script runs... the first two lines of the script are...

<?php
   $DEBUG = TRUE;
   if($DEBUG){ error_log("\n\n\n", 0); error_log("\n\n> SEARCH MEMBER SCRIPT", 0); }
   error_log(print_r($_REQUEST, TRUE), 0);

Which logs the output...

 > SEARCH MEMBER SCRIPT
[Mon May 27 15:42:22 2013] Array
(
)

If I add another print for $_POST , the same...

If I follow... comments and just make my script print out some globals...

I get the following

array(0) {
}
array(0) {
}
array(0) {
}
array(25) {
  ["DOCUMENT_ROOT"]=>
  string(7) "C:\TJJT"
  ["REMOTE_ADDR"]=>
  string(3) "::1"
  ["REMOTE_PORT"]=>
  string(5) "59543"
  ["SERVER_SOFTWARE"]=>
  string(29) "PHP 5.4.14 Development Server"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["SERVER_NAME"]=>
  string(9) "localhost"
  ["SERVER_PORT"]=>
  string(4) "8000"
  ["REQUEST_URI"]=>
  string(31) "/php_database/search_member.php"
  ["REQUEST_METHOD"]=>
  string(4) "POST"
  ["SCRIPT_NAME"]=>
  string(31) "/php_database/search_member.php"
  ["SCRIPT_FILENAME"]=>
  string(38) "C:\TJJT\php_database\search_member.php"
  ["PHP_SELF"]=>
  string(31) "/php_database/search_member.php"
  ["HTTP_HOST"]=>
  string(14) "localhost:8000"
  ["HTTP_CONNECTION"]=>
  string(10) "keep-alive"
  ["HTTP_CONTENT_LENGTH"]=>
  string(2) "75"
  ["HTTP_ORIGIN"]=>
  string(21) "http://localhost:8000"
  ["HTTP_USER_AGENT"]=>
  string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36"
  ["HTTP_CONTENT_TYPE"]=>
  string(33) "application/x-www-form-urlencoded"
  ["HTTP_ACCEPT"]=>
  string(3) "*/*"
  ["HTTP_REFERER"]=>
  string(48) "http://localhost:8000/php_database/db_search.php"
  ["HTTP_ACCEPT_ENCODING"]=>
  string(17) "gzip,deflate,sdch"
  ["HTTP_ACCEPT_LANGUAGE"]=>
  string(14) "en-US,en;q=0.8"
  ["HTTP_COOKIE"]=>
  string(50) "PHPSESSID=b1e2eb2ba3d687bfeadee6bc17e1b994c214cd3a"
  ["REQUEST_TIME_FLOAT"]=>
  float(1369666751.2246)
  ["REQUEST_TIME"]=>
  int(1369666751)
}
string(75) "forename=xfvz&surname=zxcvzx&nonce=276083038fdd9a932ff06b94e5786fdd840873a4"

At the bottom of the above i can clearly see...

string(75) "forename=xfvz&surname=zxcvzx&nonce=276083038fdd9a932ff06b94e5786fdd840873a4"

Wow. So the $HTTP_RAW_POST_DATA does actually hit the PHP development server... but for some reason it is not used to populate $_POST .

Your client-side code is sending a POST request with (apparently) the expected data as request body and the information reaches PHP. However, the information ends up in $HTTP_RAW_POST_DATA rather than $_POST .

The manual page for the always_populate_raw_post_data directive explains this:

Always populate the $HTTP_RAW_POST_DATA containing the raw POST data. Otherwise, the variable is populated only with unrecognized MIME type of the data . However, the preferred method for accessing the raw POST data is php://input. $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data".

This provides a possible explanation: PHP is failing to recognise the Content-Type request header.

There's a report in the PHP bug tracker ( POST values in Google Chrome XHR ) where this explanation is suggested. The reason might be that Google Chrome does something different than other browsers and PHP's builtin server cannot handle it. I think you should follow this line of research.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM