2

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.

4

1 回答 1

1

您的客户端代码正在发送一个带有(显然)预期数据作为请求正文的 POST 请求,并且信息到达 PHP。但是,信息最终以$HTTP_RAW_POST_DATA而不是$_POST.

always_populate_raw_post_data指令的手册页解释了这一点:

始终填充$HTTP_RAW_POST_DATA包含原始 POST 数据的内容。否则,该变量仅填充有无法识别的 MIME 类型的数据。但是,访问原始 POST 数据的首选方法是 php://input。$HTTP_RAW_POST_DATA 不适用于 enctype="multipart/form-data"。

这提供了一种可能的解释:PHP 无法识别Content-Type请求标头。

PHP 错误跟踪器中有一份报告(Google Chrome XHR 中的 POST 值)建议进行此解释。原因可能是 Google Chrome 的功能与其他浏览器不同,而 PHP 的内置服务器无法处理它。我认为你应该遵循这条研究路线。

于 2013-05-27T16:28:02.910 回答