天天看點

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

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

對于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”

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

讀取POST資料

不能用于multipart/form-data類型

php://input VS $HTTP_RAW_POST_DATA

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

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

<code>1</code>

<code>@file 192.168.0.6:/phpinput_server.php 列印出接收到的資料</code>

<code>2</code>

<code>@file 192.168.0.8:/phpinput_post.php 模拟以POST方法送出表單資料</code>

<code>3</code>

<code>@file 192.168.0.8:/phpinput_xmlrpc.php 模拟以POST方法發出xmlrpc請求.</code>

<code>4</code>

<code>@file 192.168.0.8:/phpinput_get.php 模拟以GET方法送出表單表數</code>

phpinput_server.php與phpinput_post.php

<code>//@file phpinput_server.php</code>

<code>echo</code> <code>"-------\$_POST------------------\n"</code><code>;</code>

<code>echo</code> <code>var_dump(</code><code>$_POST</code><code>) . </code><code>"\n"</code><code>;</code>

<code>5</code>

<code>6</code>

<code>echo</code> <code>$raw_post_data</code> <code>. </code><code>"\n"</code><code>;</code>

<code>01</code>

<code>//@file phpinput_post.php</code>

<code>02</code>

<code>$http_entity_body</code> <code>= </code><code>'n='</code> <code>. urldecode(</code><code>'perfgeeks'</code><code>) . </code><code>'&amp;p='</code> <code>. urldecode(</code><code>'7788'</code><code>);</code>

<code>03</code>

<code>$http_entity_type</code> <code>= </code><code>'application/x-www-form-urlencoded'</code><code>;</code>

<code>04</code>

<code>$http_entity_length</code> <code>= </code><code>strlen</code><code>(</code><code>$http_entity_body</code><code>);</code>

<code>05</code>

<code>$host</code> <code>= </code><code>'192.168.0.6'</code><code>;</code>

<code>06</code>

<code>$port</code> <code>= 80;</code>

<code>07</code>

<code>$path</code> <code>= </code><code>'/phpinput_server.php'</code><code>;</code>

<code>08</code>

<code>$fp</code> <code>= </code><code>fsockopen</code><code>(</code><code>$host</code><code>, </code><code>$port</code><code>, </code><code>$error_no</code><code>, </code><code>$error_desc</code><code>, 30);</code>

<code>09</code>

<code>if</code> <code>(</code><code>$fp</code><code>) {</code>

<code>10</code>

<code>  </code><code>fputs</code><code>(</code><code>$fp</code><code>, </code><code>"POST {$path} HTTP/1.1\r\n"</code><code>);</code>

<code>11</code>

<code>  </code><code>fputs</code><code>(</code><code>$fp</code><code>, </code><code>"Host: {$host}\r\n"</code><code>);</code>

<code>12</code>

<code>  </code><code>fputs</code><code>(</code><code>$fp</code><code>, </code><code>"Content-Type: {$http_entity_type}\r\n"</code><code>);</code>

<code>13</code>

<code>  </code><code>fputs</code><code>(</code><code>$fp</code><code>, </code><code>"Content-Length: {$http_entity_length}\r\n"</code><code>);</code>

<code>14</code>

<code>  </code><code>fputs</code><code>(</code><code>$fp</code><code>, </code><code>"Connection: close\r\n\r\n"</code><code>);</code>

<code>15</code>

<code>  </code><code>fputs</code><code>(</code><code>$fp</code><code>, </code><code>$http_entity_body</code> <code>. </code><code>"\r\n\r\n"</code><code>);</code>

<code>16</code>

<code> </code> 

<code>17</code>

<code>  </code><code>while</code> <code>(!</code><code>feof</code><code>(</code><code>$fp</code><code>)) {</code>

<code>18</code>

<code>    </code><code>$d</code> <code>.= </code><code>fgets</code><code>(</code><code>$fp</code><code>, 4096);</code>

<code>19</code>

<code>  </code><code>}</code>

<code>20</code>

<code>  </code><code>fclose(</code><code>$fp</code><code>);</code>

<code>21</code>

<code>  </code><code>echo</code> <code>$d</code><code>;</code>

<code>22</code>

<code>}</code>

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

<code>@php /phpinput_post.php</code>

<code>HTTP/1.1 200 OK</code>

<code>Date</code><code>: Thu, 08 Apr 2010 03:23:36 GMT</code>

<code>Server: Apache/2.2.3 (CentOS)</code>

<code>X-Powered-By: PHP/5.1.6</code>

<code>Content-Length: 160</code>

<code>Connection: close</code>

<code>Content-Type: text/html; charset=UTF-8</code>

<code>-------</code><code>$_POST</code><code>------------------</code>

<code>array</code><code>(2) {</code>

<code>  </code><code>[</code><code>"n"</code><code>]=&gt; string(9) </code><code>"perfgeeks"</code>

<code>  </code><code>[</code><code>"p"</code><code>]=&gt; string(4) </code><code>"7788"</code>

<code>-------php:</code><code>//input-------------</code>

<code>n=perfgeeks&amp;p=7788</code>

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

<code>T 192.168.0.8:57846 -&gt; 192.168.0.6:80 [AP]</code>

<code>  </code><code>POST /phpinput_server.php HTTP/1.1..</code>

<code>  </code><code>Host: 192.168.0.6..Content-Type: application/x-www-form-urlencoded..Co</code>

<code>  </code><code>ntent-Length: 18..Connection: close....n=perfgeeks&amp;p=7788....</code>

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

$_POST資料,php://input 資料與httpd entity body資料是“一緻”的。

http請求中的Content-Type是application/x-www-form-urlencoded ,它表示http請求body中的資料是使用http的post方法送出的表單資料,并且進行了urlencode()處理。

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

<code>//@file phpinput_xmlrpc.php</code>

<code>$http_entity_body</code> <code>= </code><code>"\n\n   jt_userinfo\n"</code><code>;</code>

<code>$http_entity_type</code> <code>= </code><code>'text/html'</code><code>;</code>

同樣地,讓我們來執行這個測試腳本:

<code>@php /phpinput_xmlrcp.php</code>

<code>Date</code><code>: Thu, 08 Apr 2010 03:47:18 GMT</code>

<code>Content-Length: 154</code>

<code>array</code><code>(0) {</code>

<code>&lt;?xml version=</code><code>"1.0"</code><code>&gt;</code>

<code>&lt;methodcall&gt;</code>

<code>   </code><code>&lt;name&gt;jt_userinfo&lt;/name&gt;</code>

<code>&lt;/methodcall&gt;</code>

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

<code>T 192.168.0.8:45570 -&gt; 192.168.0.6:80 [AP]</code>

<code>  </code><code>Host: 192.168.0.6..Content-Type: text/html..Content-Length: 75..Connec</code>

<code>  </code><code>tion: close....&lt;?xml version=</code><code>"1.0"</code><code>&gt;.&lt;methodcall&gt;.   &lt;name&gt;jt_userinfo&lt;</code>

<code>  </code><code>/name&gt;.&lt;/methodcall&gt;....</code>

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

http請求中的Content-Type是text/xml。它表示http請求中的body資料是xml資料格式。

服務端$_POST列印出來的是一個空數組,即與http entity body不一緻了。這跟上個例子不一樣了,這裡的Content-Type是text/xml,而不是application/x-www-form-urlencoded

而php://input資料還是跟http entity body資料一緻。也就是php://input資料和$_POST資料不一緻了。

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

<code>echo</code> <code>"-------\$_GET------------------\n"</code><code>;</code>

<code>echo</code> <code>var_dump(</code><code>$_GET</code><code>) . </code><code>"\n"</code><code>;</code>

<code>//@file phpinput_get.php</code>

<code>$query_path</code> <code>= </code><code>'n='</code> <code>. urldecode(</code><code>'perfgeeks'</code><code>) . </code><code>'&amp;p='</code> <code>. urldecode(</code><code>'7788'</code><code>);</code>

<code>$d</code> <code>= </code><code>''</code><code>;</code>

<code>  </code><code>fputs</code><code>(</code><code>$fp</code><code>, </code><code>"GET {$path}?{$query_path} HTTP/1.1\r\n"</code><code>);</code>

<code> </code><code>}</code>

同樣,我們執行下一phpinput_get.php測試腳本,它模拟了一個通常情況下的GET方法送出表單資料。

<code>@php /phpinput_get.php</code>

<code>Date</code><code>: Thu, 08 Apr 2010 07:38:15 GMT</code>

<code>Content-Length: 141</code>

<code>-------</code><code>$_GET</code><code>------------------</code>

<code>  </code><code>[</code><code>"n"</code><code>]=&gt;</code>

<code>  </code><code>string(9) </code><code>"perfgeeks"</code>

<code>  </code><code>[</code><code>"p"</code><code>]=&gt;</code>

<code>  </code><code>string(4) </code><code>"7788"</code>

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

<code>T 192.168.0.8:36775 -&gt; 192.168.0.6:80 [AP]</code>

<code>  </code><code>GET /phpinput_server.php?n=perfgeeks&amp;p=7788 HTTP/1.1..</code>

<code>  </code><code>Host: 192.168.0.6..Connection: close....</code>

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

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

Content-Type取值為application/x-www-form-urlencoded時,php會将http請求body相應資料會填入到數組$_POST,填入到$_POST數組中的資料是進行urldecode()解析的結果。(其實,除了該Content-Type,還有multipart/form-data表示資料是表單資料,稍後我們介紹)

php://input資料,隻要Content-Type不為multipart/form-data(該條件限制稍後會介紹)。那麼php://input資料與http entity body部分資料是一緻的。該部分相一緻的資料的長度由Content-Length指定。

僅當Content-Type為application/x-www-form-urlencoded且送出方法是POST方法時,$_POST資料與php://input資料才是”一緻”(打上引号,表示它們格式不一緻,内容一緻)的。其它情況,它們都不一緻。

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

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

<code>&lt;form enctype=</code><code>"multipart/form-data"</code> <code>action=</code><code>"phpinput_server.php"</code><code>method=</code><code>"POST"</code> <code>&gt;</code>

<code>    </code><code>&lt;input type=</code><code>"text"</code> <code>name=</code><code>"n"</code>  <code>/&gt;</code>

<code>    </code><code>&lt;input type=</code><code>"file"</code> <code>name=</code><code>"f"</code> <code>/&gt;</code>

<code>    </code><code>&lt;input type=</code><code>"submit"</code> <code>value=</code><code>"upload now"</code> <code>/&gt;</code>

<code>&lt;/form&gt;</code>

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

<code>array</code><code>(1) { [</code><code>"n"</code><code>]=&gt; string(9) </code><code>"perfgeeks"</code> <code>}</code>

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

<code>########</code>

<code>T 192.168.0.8:3981 -&gt; 192.168.0.6:80 [AP]</code>

<code>  </code><code>POST /phpinput_server.php HTTP/1.1..Host: 192.168.0.6..Connection: kee</code>

<code>  </code><code>p-alive..User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) A</code>

<code>  </code><code>ppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2..Re</code>

<code>  </code><code>ferer: http:</code><code>//192.168.0.6/phpinput_server.php..Content-Length: 306..Ca</code>

<code>  </code><code>che-Control: max-age=0..Origin: http:</code><code>//192.168.0.6..Content-Type: mult</code>

<code>  </code><code>ipart/form-data; boundary=----WebKitFormBoundarybLQwkp4opIEZn1fA..Acce</code>

<code>  </code><code>pt: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q</code>

<code>  </code><code>=0.8,image/png,*/*;q=0.5..Accept-Encoding: gzip,deflate,sdch..Accept-L</code>

<code>  </code><code>anguage: zh-CN,zh;q=0.8..Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3..Cook</code>

<code>  </code><code>ie: SESS3b0e658f87cf58240de13ab43a399df6=lju6o5bg8u04lv1ojugm2ccic6...</code>

<code>  </code><code>.</code>

<code>##</code>

<code>  </code><code>------WebKitFormBoundarybLQwkp4opIEZn1fA..Content-Disposition: form-da</code>

<code>  </code><code>ta; name=</code><code>"n"</code><code>....perfgeeks..------WebKitFormBoundarybLQwkp4opIEZn1fA..C</code>

<code>  </code><code>ontent-Disposition: form-data; name=</code><code>"f"</code><code>; filename=</code><code>"test.txt"</code><code>..Content-</code>

<code>  </code><code>Type: text/plain....i am file..multipart/form-data..------WebKitFormBo</code>

<code>  </code><code>undarybLQwkp4opIEZn1fA--..</code>

從響應輸出來比對,$_POST資料跟請求送出資料相符,即$_POST = array(‘n’ =&gt; ‘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已經有一定深度地了解了。那麼$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改變一下,可以驗證上述内容:

<code>$rtn</code> <code>= (</code><code>$raw_post_data</code> <code>== </code><code>$HTTP_RAW_POST_DATA</code><code>) ? 1 : 0;</code>

<code>echo</code> <code>$rtn</code><code>;</code>

執行測試腳本:

<code>@php phpinput_post.php</code>

<code>@php phpinput_get.php</code>

<code>@php phpinput_xmlrpc.php</code>

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

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

php://input 可以讀取http entity body中指定長度的值,由Content-Length指定長度,不管是POST方式或者GET方法送出過來的資料。但是,一般GET方法送出資料時,http request entity body部分都為空。

php://input 與$HTTP_RAW_POST_DATA讀取的資料是一樣的,都隻讀取Content-Type不為multipart/form-data的資料。

Coentent-Type僅在取值為application/x-www-data-urlencoded和multipart/form-data兩種情況下,PHP才會将http請求資料包中相應的資料填入全局變量$_POST

PHP不能識别的Content-Type類型的時候,會将http請求包中相應的資料填入變量$HTTP_RAW_POST_DATA

隻有Coentent-Type不為multipart/form-data的時候,PHP不會将http請求資料包中的相應資料填入php://input,否則其它情況都會。填入的長度,由Coentent-Length指定。

隻有Content-Type為application/x-www-data-urlencoded時,php://input資料才跟$_POST資料相一緻。

php://input資料總是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更湊效,且不需要特殊設定php.ini

PHP會将PATH字段的query_path部分,填入全局變量$_GET。通常情況下,GET方法送出的http請求,body為空。

如何聯系我:【萬裡虎】www.bravetiger.cn

【QQ】3396726884 (咨詢問題100元起,幫助解決問題500元起)

【部落格】http://www.cnblogs.com/kenshinobiy/