深入剖析PHP輸入流php://input(與POST/GET的區別)

suboysugar發表於2015-02-09

PHP輸入流php://input

轉:http://www.nowamagic.net/academy/detail/12220520

 

在使用xml-rpc的時候,server端獲取client資料,主要是通過php輸入流input,而不是$_POST陣列。所以,這裡主要探討php輸入流php://input

對於php://input介紹,PHP官方手冊文件有一段話對它進行了很明確地概述:

“php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype=”multipart/form-data”.

翻譯過來,是這樣:

“php://input可以讀取沒有處理過的POST資料。相較於$HTTP_RAW_POST_DATA而言,它給記憶體帶來的壓力較小,並且不需要特殊的php.ini設定。php://input不能用於enctype=multipart/form-data”

我們應該怎麼去理解這段概述呢?我把它劃分為三部分,逐步去理解:

  1. 讀取POST資料
  2. 不能用於multipart/form-data型別
  3. php://input VS $HTTP_RAW_POST_DATA

讀取POST資料

PHPer們一定很熟悉$_POST這個內建變數。$_POST與php://input存在哪些關聯與區別呢?另外,客戶端向服務端互動資料,最常用的方法除了POST之外,還有GET。既然php://input作為PHP輸入流,它能讀取GET資料嗎?這二個問題正是我們這節需要探討的主要內容。

經驗告訴我們,從測試與觀察中總結,會是一個很湊效的方法。這裡,我寫了幾個指令碼來幫助我們測試。

1 @file 192.168.0.6:/phpinput_server.php 列印出接收到的資料
2 @file 192.168.0.8:/phpinput_post.php 模擬以POST方法提交表單資料
3 @file 192.168.0.8:/phpinput_xmlrpc.php 模擬以POST方法發出xmlrpc請求.
4 @file 192.168.0.8:/phpinput_get.php 模擬以GET方法提交表單表數

phpinput_server.php與phpinput_post.php

1 //@file phpinput_server.php
2 $raw_post_data file_get_contents(`php://input``r`);
3 echo "-------$_POST------------------
"
;
4 echo var_dump($_POST) . "
"
;
5 echo "-------php://input-------------
"
;
6 echo $raw_post_data "
"
;
01 //@file phpinput_post.php
02 $http_entity_body `n=` . urldecode(`perfgeeks`) . `&p=` . urldecode(`7788`);
03 $http_entity_type `application/x-www-form-urlencoded`;
04 $http_entity_length strlen($http_entity_body);
05 $host `192.168.0.6`;
06 $port = 80;
07 $path `/phpinput_server.php`;
08 $fp fsockopen($host$port$error_no$error_desc, 30);
09 if ($fp) {
10   fputs($fp"POST {$path} HTTP/1.1
"
);
11   fputs($fp"Host: {$host}
"
);
12   fputs($fp"Content-Type: {$http_entity_type}
"
);
13   fputs($fp"Content-Length: {$http_entity_length}
"
);
14   fputs($fp"Connection: close

"

);
15   fputs($fp$http_entity_body "

"

);
16   
17   while (!feof($fp)) {
18     $d .= fgets($fp, 4096);
19   }
20   fclose($fp);
21   echo $d;
22 }

我們可以通過使用工具ngrep抓取http請求包(因為我們需要探知的是php://input,所以我們這裡只抓取http Request資料包)。我們來執行測試指令碼phpinput_post.php

01 @php /phpinput_post.php
02 HTTP/1.1 200 OK
03 Date: Thu, 08 Apr 2010 03:23:36 GMT
04 Server: Apache/2.2.3 (CentOS)
05 X-Powered-By: PHP/5.1.6
06 Content-Length: 160
07 Connection: close
08 Content-Type: text/html; charset=UTF-8
09 -------$_POST------------------
10 array(2) {
11   ["n"]=> string(9) "perfgeeks"
12   ["p"]=> string(4) "7788"
13 }
14 -------php://input-------------
15 n=perfgeeks&p=7788

通過ngrep抓到的http請求包如下:

1 T 192.168.0.8:57846 -> 192.168.0.6:80 [AP]
2   POST /phpinput_server.php HTTP/1.1..
3   Host: 192.168.0.6..Content-Type: application/x-www-form-urlencoded..Co
4   ntent-Length: 18..Connection: close....n=perfgeeks&p=7788....

仔細觀察,我們不難發現:

  1. $_POST資料,php://input 資料與httpd entity body資料是“一致”的。
  2. http請求中的Content-Type是application/x-www-form-urlencoded ,它表示http請求body中的資料是使用http的post方法提交的表單資料,並且進行了urlencode()處理。

(注:注意加粗部分內容,下文不再提示)。

我們再來看看指令碼phpinput_xmlrpc.php的原檔案內容,它模擬了一個POST方法提交的xml-rpc請求。

01 //@file phpinput_xmlrpc.php
02 $http_entity_body "

   jt_userinfo
"

;
03 $http_entity_type `text/html`;
04 $http_entity_length strlen($http_entity_body);
05 $host `192.168.0.6`;
06 $port = 80;
07 $path `/phpinput_server.php`;
08 $fp fsockopen($host$port$error_no$error_desc, 30);
09 if ($fp) {
10   fputs($fp"POST {$path} HTTP/1.1
"
);
11   fputs($fp"Host: {$host}
"
);
12   fputs($fp"Content-Type: {$http_entity_type}
"
);
13   fputs($fp"Content-Length: {$http_entity_length}
"
);
14   fputs($fp"Connection: close

"

);
15   fputs($fp$http_entity_body "

"

);
16   while (!feof($fp)) {
17     $d .= fgets($fp, 4096);
18   }
19   
20   fclose($fp);
21   echo $d;
22 }

同樣地,讓我們來執行這個測試指令碼:

01 @php /phpinput_xmlrcp.php
02 HTTP/1.1 200 OK
03 Date: Thu, 08 Apr 2010 03:47:18 GMT
04 Server: Apache/2.2.3 (CentOS)
05 X-Powered-By: PHP/5.1.6
06 Content-Length: 154
07 Connection: close
08 Content-Type: text/html; charset=UTF-8
09  
10 -------$_POST------------------
11 array(0) {
12 }
13  
14 -------php://input-------------
15 <?xml version="1.0">
16 <methodcall>
17    <name>jt_userinfo</name>
18 </methodcall>

執行這個指令碼的時候,我們通過ngrep抓取的http請求資料包如下:

1 T 192.168.0.8:45570 -> 192.168.0.6:80 [AP]
2   POST /phpinput_server.php HTTP/1.1..
3   Host: 192.168.0.6..Content-Type: text/html..Content-Length: 75..Connec
4   tion: close....<?xml version="1.0">.<methodcall>.   <name>jt_userinfo<
5   /name>.</methodcall>....

同樣,我樣也可以很容易地發現:

  1. http請求中的Content-Type是text/xml。它表示http請求中的body資料是xml資料格式。
  2. 服務端$_POST列印出來的是一個空陣列,即與http entity body不一致了。這跟上個例子不一樣了,這裡的Content-Type是text/xml,而不是application/x-www-form-urlencoded
  3. 而php://input資料還是跟http entity body資料一致。也就是php://input資料和$_POST資料不一致了。

我們再來看看通過GET方法提交表單資料的情況,php://input能不能讀取到GET方法的表單資料?在這裡,我們稍加改動一下phpinput_server.php檔案,將$_POST改成$_GET。

1 //@file phpinput_server.php
2 $raw_post_data file_get_contents(`php://input``r`);
3 echo "-------$_GET------------------
"
;
4 echo var_dump($_GET) . "
"
;
5 echo "-------php://input-------------
"
;
6 echo $raw_post_data "
"
;
01 //@file phpinput_get.php
02 $query_path `n=` . urldecode(`perfgeeks`) . `&p=` . urldecode(`7788`);
03 $host `192.168.0.6`;
04 $port = 80;
05 $path `/phpinput_server.php`;
06 $d ``;
07 $fp fsockopen($host$port$error_no$error_desc, 30);
08 if ($fp) {
09   fputs($fp"GET {$path}?{$query_path} HTTP/1.1
"
);
10   fputs($fp"Host: {$host}
"
);
11   fputs($fp"Connection: close

"

);
12   
13   while (!feof($fp)) {
14     $d .= fgets($fp, 4096);
15   }
16   fclose($fp);
17   echo $d;
18  }

同樣,我們執行下一phpinput_get.php測試指令碼,它模擬了一個通常情況下的GET方法提交表單資料。

01 @php /phpinput_get.php
02 HTTP/1.1 200 OK
03 Date: Thu, 08 Apr 2010 07:38:15 GMT
04 Server: Apache/2.2.3 (CentOS)
05 X-Powered-By: PHP/5.1.6
06 Content-Length: 141
07 Connection: close
08 Content-Type: text/html; charset=UTF-8
09  
10 -------$_GET------------------
11 array(2) {
12   ["n"]=>
13   string(9) "perfgeeks"
14   ["p"]=>
15   string(4) "7788"
16 }
17  
18 -------php://input-------------

在這個時候,使用ngrep工具,捕獲的相應的http請求資料包如下:

1 T 192.168.0.8:36775 -> 192.168.0.6:80 [AP]
2   GET /phpinput_server.php?n=perfgeeks&p=7788 HTTP/1.1..
3   Host: 192.168.0.6..Connection: close....

比較POST方法提交的http請求,通常GET方法提交的請求中,entity body為空。同時,不會指定Content-Type和Content-Length。但是,如果強硬資料http entity body,並指明正確地Content-Type和Content-Length,那麼php://input還可是讀取得到http entity body資料,但不是$_GET資料。

所根據,上面幾個探測,我們可以作出以下總結:

  1. Content-Type取值為application/x-www-form-urlencoded時,php會將http請求body相應資料會填入到陣列$_POST,填入到$_POST陣列中的資料是進行urldecode()解析的結果。(其實,除了該Content-Type,還有multipart/form-data表示資料是表單資料,稍後我們介紹)
  2. php://input資料,只要Content-Type不為multipart/form-data(該條件限制稍後會介紹)。那麼php://input資料與http entity body部分資料是一致的。該部分相一致的資料的長度由Content-Length指定。
  3. 僅當Content-Type為application/x-www-form-urlencoded且提交方法是POST方法時,$_POST資料與php://input資料才是”一致”(打上引號,表示它們格式不一致,內容一致)的。其它情況,它們都不一致。
  4. php://input讀取不到$_GET資料。是因為$_GET資料作為query_path寫在http請求頭部(header)的PATH欄位,而不是寫在http請求的body部分。

這也幫助我們理解了,為什麼xml_rpc服務端讀取資料都是通過file_get_contents(‘php://input’, ‘r’)。而不是從$_POST中讀取,正是因為xml_rpc資料規格是xml,它的Content-Type是text/xml。

php://input碰到了multipart/form-data

上傳檔案的時候,表單的寫法是這樣的:

1 <form enctype="multipart/form-data" action="phpinput_server.php"method="POST" >
2     <input type="text" name="n"  />
3     <input type="file" name="f" />
4     <input type="submit" value="upload now" />
5 </form>

那麼,enctype=multipart/form-data這裡的意義,就是將該次http請求頭部(head)中的Content-Type設定為multipart/form-data。請查閱RFC1867對它的描述。multipart/form-data也表示以POST方法提交表單資料,它還伴隨了檔案上傳,所以會跟application/x-www-form-urlencoded資料格式不一樣。它會以一更種更合理的,更高效的資料格式傳遞給服務端。我們提交該表單資料,並且列印出響應結果,如下:

1 -------$_POST------------------
2 array(1) { ["n"]=> string(9) "perfgeeks" }
3 -------php://input-------------

同時,我們通過ngrep抓取的相應的http請求資料包如下:

01 ########
02 T 192.168.0.8:3981 -> 192.168.0.6:80 [AP]
03   POST /phpinput_server.php HTTP/1.1..Host: 192.168.0.6..Connection: kee
04   p-alive..User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) A
05   ppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2..Re
06   ferer: http://192.168.0.6/phpinput_server.php..Content-Length: 306..Ca
07   che-Control: max-age=0..Origin: http://192.168.0.6..Content-Type: mult
08   ipart/form-data; boundary=----WebKitFormBoundarybLQwkp4opIEZn1fA..Acce
09   pt: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q
10   =0.8,image/png,*/*;q=0.5..Accept-Encoding: gzip,deflate,sdch..Accept-L
11   anguage: zh-CN,zh;q=0.8..Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3..Cook
12   ie: SESS3b0e658f87cf58240de13ab43a399df6=lju6o5bg8u04lv1ojugm2ccic6...
13   .
14 ##
15 T 192.168.0.8:3981 -> 192.168.0.6:80 [AP]
16   ------WebKitFormBoundarybLQwkp4opIEZn1fA..Content-Disposition: form-da
17   ta; name="n"....perfgeeks..------WebKitFormBoundarybLQwkp4opIEZn1fA..C
18   ontent-Disposition: form-data; name="f"; filename="test.txt"..Content-
19   Type: text/plain....i am file..multipart/form-data..------WebKitFormBo
20   undarybLQwkp4opIEZn1fA--..
21 ##

從響應輸出來比對,$_POST資料跟請求提交資料相符,即$_POST = array(‘n’ => ‘perfgeeks’)。這也跟http請求body中的資料相呼應,同時說明PHP把相應的資料填入$_POST全域性變數。而php://input輸出為空,沒有輸出任何東西,儘管http請求資料包中body不為空。這表示,當Content-Type為multipart/form-data的時候,即便http請求body中存在資料,php://input也為空,PHP此時,不會把資料填入php://input流。所以,可以確定: php://input不能用於讀取enctype=multipart/form-data資料。

我們再比較這次通過ngrep抓取的http請求資料包,我們會發現,最大不同的一點是Content-Type後面跟了boundary定義了資料的分界符,bounday是隨機生成的。另外一個大不一樣的,就是http entity body中的資料組織結構不一樣了。

上一節,我們概述了,當Content-Type為application/x-www-form-urlencoded時,php://input和$_POST資料是“一致”的,為其它Content-Type的時候,php://input和$_POST資料資料是不一致的。因為只有在Content-Type為application/x-www-form-urlencoded或者為multipart/form-data的時候,PHP才會將http請求資料包中的body相應部分資料填入$_POST全域性變數中,其它情況PHP都忽略。而php://input除了在資料型別為multipart/form-data之外為空外,其它情況都可能不為空。通過這一節,我們更加明白了php://input與$_POST的區別與聯絡。所以,再次確認,php://input無法讀取enctype=multipart/form-data資料,當php://input遇到它時,永遠為空,即便http entity body有資料。

php://input VS $http_raw_post_data

相信大家對php://input已經有一定深度地瞭解了。那麼$http_raw_post_data是什麼呢?$http_raw_post_data是PHP內建的一個全域性變數。它用於,PHP在無法識別的Content-Type的情況下,將POST過來的資料原樣地填入變數$http_raw_post_data。它同樣無法讀取Content-Type為multipart/form-data的POST資料。需要設定php.ini中的always_populate_raw_post_data值為On,PHP才會總把POST資料填入變數$http_raw_post_data。

把指令碼phpinput_server.php改變一下,可以驗證上述內容:

1 $raw_post_data file_get_contents(`php://input``r`);
2 $rtn = ($raw_post_data == $HTTP_RAW_POST_DATA) ? 1 : 0;
3 echo $rtn;

執行測試指令碼:

1 @php phpinput_post.php
2 @php phpinput_get.php
3 @php phpinput_xmlrpc.php

得出的結果輸出都是一樣的,即都為1,表示php://input和$HTTP_RAW_POST_DATA是相同的。至於對記憶體的壓力,我們這裡就不做細緻地測試了。有興趣的,可以通過xhprof進行測試和觀察。

以此,我們這節可以總結如下:

  1. php://input 可以讀取http entity body中指定長度的值,由Content-Length指定長度,不管是POST方式或者GET方法提交過來的資料。但是,一般GET方法提交資料時,http request entity body部分都為空。
  2. php://input 與$HTTP_RAW_POST_DATA讀取的資料是一樣的,都只讀取Content-Type不為multipart/form-data的資料。

小結

  1. Coentent-Type僅在取值為application/x-www-data-urlencoded和multipart/form-data兩種情況下,PHP才會將http請求資料包中相應的資料填入全域性變數$_POST
  2. PHP不能識別的Content-Type型別的時候,會將http請求包中相應的資料填入變數$HTTP_RAW_POST_DATA
  3. 只有Coentent-Type不為multipart/form-data的時候,PHP不會將http請求資料包中的相應資料填入php://input,否則其它情況都會。填入的長度,由Coentent-Length指定。
  4. 只有Content-Type為application/x-www-data-urlencoded時,php://input資料才跟$_POST資料相一致。
  5. php://input資料總是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更湊效,且不需要特殊設定php.ini
  6. PHP會將PATH欄位的query_path部分,填入全域性變數$_GET。通常情況下,GET方法提交的http請求,body為空。

如何聯絡我:【萬里虎】www.bravetiger.cn
【QQ】3396726884 (諮詢問題100元起,幫助解決問題500元起)
【部落格】http://www.cnblogs.com/kenshinobiy/


相關文章