Pour une introduction à php://input, le document officiel du manuel PHP contient un paragraphe qui le décrit clairement. 同樣,我們執(zhí)行下一phpinput_get.php測試腳本,它模擬了一個通常情況下的GET方法提交表單數(shù)據(jù)。 那么,enctype=multipart/form-data這里的意義,就是將該次http請求頭部(head)中的Content-Type設(shè)置為multipart/form-data。請查閱RFC1867對 它的描述。multipart/form-data也表示以POST方法提交表單數(shù)據(jù),它還伴隨了文件上傳,所以會跟application/x- www-form-urlencoded數(shù)據(jù)格式不一樣。它會以一更種更合理的,更高效的數(shù)據(jù)格式傳遞給服務(wù)端。我們提交該表單數(shù)據(jù),并且打印出響應(yīng)結(jié) 果,如下:
? php://input vous permet de lire des données POST brutes. C'est une alternative moins gourmande en mémoire que $HTTP_RAW_POST_DATA et ne nécessite aucune directive php.ini spéciale. php://input n'est pas disponible avec. enctype="multipart/form-data".
Traduit, ceci est?:
"php://input peut lire les données POST non traitées. Comparé à $HTTP_RAW_POST_DATA, il exerce moins de pression sur la mémoire et ne nécessite pas de paramètres php.ini spéciaux. php://input ne peut pas être utilisé pour enctype=multipart/form-data ?
Comment devrions-nous comprendre cet aper?u ?! Je l'ai divisé en trois parties et je l'ai compris étape par étape
Lisez les données POST.
ne peut pas être utilisé pour le type de données multipart/form
php://input VS $HTTP_RAW_POST_DATA
Lire les données POST
Les PHPers doivent être familiers avec la variable intégrée $_POST $. Quelles sont les relations et les différences entre _POST et php://input ? De plus, la méthode la plus couramment utilisée par le client pour interagir avec le serveur est GET, en plus de POST puisque php://input est un flux d'entrée PHP. , il peut être utilisé. Lire les données GET ? Ces deux questions sont le contenu principal dont nous devons discuter dans cette section
L'expérience nous dit que ce sera une méthode très efficace pour résumer les tests et l'observation. quelques-uns. Un script pour nous aider à tester.
@file 192.168.0.6:/phpinput_server.php imprime les données re?ues
@file 192.168.0.8:/phpinput_post.php simule la soumission des données du formulaire en utilisant le Méthode POST
@file 192.168.0.8:/phpinput_xmlrpc.php simule l'émission d'une requête xmlrpc à l'aide de la méthode POST
@file 192.168.0.8:/phpinput_get.php simule la soumission d'un formulaire à l'aide de la méthode GET
phpinput_server. .php et phpinput_post.php
//@file phpinput_server.php
$raw_post_data = file_get_contents('php://input', 'r ');
echo "-------$_POST------------------n";
echo var_dump($_POST) "n". ";
echo "-------php://input-------------n";
echo $raw_post_data . "n";
? >
//@file phpinput_post.php
$http_entity_body = 'n=' . );
$http_entity_type = 'application/x-www-form-urlencoded';
$http_entity_length = strlen($http_entity_body);
$host = '192.168.0.6'; 80;
$path = '/phpinput_server.php';
$fp = fsockopen($host, $port, $error_no, $error_desc, 30); fputs($fp, "POST {$path} HTTP/1.1rn");
fputs($fp, "H?te?: {$host}rn");
fputs($fp, "Content-Type?: {$http_entity_type}rn ");
fputs($fp, "Content-Length: {$http_entity_length}rn");
fputs($fp, "Connection: closenrn");
fputs($ fp, $http_entity_body . "rnrn");
while (!feof($fp)) {
$d .= fgets($fp, 4096); $fp);
echo $d;
}
?>
Nous pouvons récupérer le paquet de requête http en utilisant l'outil ngrep (car ce que nous devons détecter est php://input, donc nous récupérons uniquement http ici (demande de paquet). Exécutons le script de test phpinput_post.php
@php /phpinput_post.php
HTTP/1.1 200 OK
Date?: jeu. 08 avril 2010 03:23:36 GMT
Serveur?: Apache /2.2.3 (CentOS)
X-Powered-By : PHP/5.1.6
Content-Length : 160
Connexion : close
Content-Type : text/html charset=UTF- ; 8
-------$_POST-----------------
array(2) {
["n"]=> string(9) "perfgeeks"
["p"]=> string(4) "7788"
}
-------php://input------ -------
n=perfgeeks&p=7788
Le paquet de requête http capturé via ngrep est le suivant?:
T 192.168.0.8:57846 -> AP ]
POST /phpinput_server.php HTTP/1.1..
H?te?: 192.168.0.6..Content-Type?: application/x-www-form-urlencoded..Co
ntent-Length?: 18. . Connexion?: close....n=perfgeeks&p=7788....
En regardant attentivement, nous pouvons facilement constater que
1, les données $_POST, les données php://input et les données du corps de l'entité httpd sont " cohérent"
2. Le type de contenu dans la requête http est application/x-www-form-urlencoded, ce qui signifie que les données dans le corps de la requête http sont les données du formulaire soumises à l'aide de la méthode post de http, et ont été traité par urlencode() .
(Remarque?: faites attention à la partie en gras, qui ne sera pas demandée ci-dessous.
Jetons un coup d'?il au contenu du fichier original du script phpinput_xmlrpc.php, qui simule une requête xml-rpc). soumis par la méthode POST.
//@file phpinput_xmlrpc.php
$http_entity_body = "nn jt_userinfon";?
$http_entity_type = 'text/html';?
$http_entity_length = strlen($http_entity_body);?
$h?te = '192.168.0.6';?
$port = 80?;?
$path = '/phpinput_server.php';?
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);?
if ($fp) {?
fputs($fp, "POST {$path} HTTP/1.1rn");?
fputs($fp, "H?te?: {$host}rn");?
fputs($fp, "Content-Type?: {$http_entity_type}rn");?
fputs($fp, "Content-Length?: {$http_entity_length}rn");?
fputs($fp, "Connexion?: closenrn");?
fputs($fp, $http_entity_body . "rnrn");?
while (!feof($fp)) {?
$d .= fgets($fp, 4096);?
}?
fclose($fp);?
écho $d;?
}?
?>?
同樣地,讓我們來執(zhí)行這個測試腳本?
@php /phpinput_xmlrcp.php?
HTTP/1.1 200 OK?
Date?:?jeudi 8 avril 2010 03:47?:?18?GMT?
Serveur?: Apache/2.2.3 (CentOS)?
X-Powered-By?: PHP/5.1.6?
Content-Length?: 154?
Connexion?: fermer?
Content-Type?: text/html ; charset=UTF-8?
-------$_POST------------------?
array(0) {?
}
-------php://input-------------?
?
?> 192.168.0.6:80 [AP]?
POST /phpinput_server.php HTTP/1.1..?
H?te?: 192.168.0.6..Content-Type?: text/html..Content-Length?: 75..Connec?
tion?: fermer....
/name>.
同樣,我樣也可以很容易地發(fā)現(xiàn)?:?
1,http請求中的Content-Type是text/xml。它表示http請求中的body數(shù)據(jù)是xml數(shù)據(jù)格式。?
2,服務(wù)端$_POST打印出來的是一個空數(shù)組,即與httpentity body不一致了。這跟Il s'agit d'un type de contenu. text/xml,而不是application/x-www-form-urlencoded?
3,而php://input數(shù)據(jù)還是跟httpentity body數(shù)據(jù)一致。也就是php://input數(shù)據(jù)和$_POST數(shù)據(jù)不一致了。
我們再來看看通過GET方法提交表單數(shù)據(jù)的情況,php://input能不能讀取到GET方法的表單數(shù)據(jù)?在Il s'agit d'un serveur PHPinput_server.php qui contient $ _POST 成$_GET。?<?php
//@file phpinput_server.php
$raw_post_data = file_get_contents('php://input', 'r');
echo "-------\$_GET------------------\n";
echo var_dump($_GET) . "\n";
echo "-------php://input-------------\n";
echo $raw_post_data . "\n";
?>
<?php
//@file phpinput_get.php
$query_path = 'n=' . urldecode('perfgeeks') . '&p=' . urldecode('7788');
$host = '192.168.0.6';
$port = 80;
$path = '/phpinput_server.php';
$d = '';
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp) {
fputs($fp, "GET {$path}?{$query_path} HTTP/1.1\r\n");
fputs($fp, "Host: {$host}\r\n");
fputs($fp, "Connection: close\r\n\r\n");
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}
?>
@php /phpinput_get.php
HTTP/1.1 200 OK
Date: Thu, 08 Apr 2010 07:38:15 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.1.6
Content-Length: 141
Connection: close
Content-Type: text/html; charset=UTF-8
-------$_GET------------------
array(2) {
["n"]=>
string(9) "perfgeeks"
["p"]=>
string(4) "7788"
}
-------php://input-------------
在這個時候,使用ngrep工具,捕獲的相應(yīng)的http請求數(shù)據(jù)包如下
T 192.168.0.8:36775 -> 192.168.0.6:80 [AP]
GET /phpinput_server.php?n=perfgeeks&p=7788 HTTP/1.1..
Host: 192.168.0.6..Connection: close....
比較POST方法提交的http請求,通常GET方法提交的請求中,entity body為空。同時,不會指定Content-Type和Content-Length。但是,如果強硬數(shù)據(jù)http entity body,并指明正確地Content-Type和Content-Length,那么php://input還可是讀取得到http entity body數(shù)據(jù),但不是$_GET數(shù)據(jù)。
所根據(jù),上面幾個探測,我們可以作出以下總結(jié):
1,Content- Type取值為application/x-www-form-urlencoded時,php會將http請求body相應(yīng)數(shù)據(jù)會填入到數(shù) 組$_POST,填入到$_POST數(shù)組中的數(shù)據(jù)是進行urldecode()解析的結(jié)果。(其實,除了該Content-Type,還有 multipart/form-data表示數(shù)據(jù)是表單數(shù)據(jù),稍后我們介紹)
2,php://input數(shù)據(jù),只要Content-Type不為 multipart/form-data(該條件限制稍后會介紹)。那么php://input數(shù)據(jù)與http entity body部分數(shù)據(jù)是一致的。該部分相一致的數(shù)據(jù)的長度由Content-Length指定。
3,僅當(dāng)Content-Type為application/x-www-form-urlencoded且提交方法是POST方法時,$_POST數(shù)據(jù)與php://input數(shù)據(jù)才是”一致”(打上引號,表示它們格式不一致,內(nèi)容一致)的。其它情況,它們都不一致。
4,php://input讀取不到$_GET數(shù)據(jù)。是因為$_GET數(shù)據(jù)作為query_path寫在http請求頭部(header)的PATH字段,而不是寫在http請求的body部分。
這也幫助我們理解了,為什么xml_rpc服務(wù)端讀取數(shù)據(jù)都是通過file_get_contents(‘php://input', ‘r')。而不是從$_POST中讀取,正是因為xml_rpc數(shù)據(jù)規(guī)格是xml,它的Content-Type是text/xml。
php://input碰到了multipart/form-data
上傳文件的時候,表單的寫法是這樣的 <form enctype="multipart/form-data" action="phpinput_server.php" method="POST" >
<input type="text" name="n" />
<input type="file" name="f" />
<input type="submit" value="upload now" />
</form>
-------$_POST------------------
array(1) { ["n"]=> string(9) "perfgeeks" }
-------php://input-------------
同時,我們通過ngrep抓取的相應(yīng)的http請求數(shù)據(jù)包如下:
########
T 192.168.0.8:3981 -> 192.168.0.6:80 [AP]
POST /phpinput_server.php HTTP/1.1..Host: 192.168.0.6..Connection: kee
p-alive..User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) A
ppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2..Re
ferer: http://192.168.0.6/phpinput_server.php..Content-Length: 306..Ca
che-Control: max-age=0..Origin: http://192.168.0.6..Content-Type: mult
ipart/form-data; boundary=----WebKitFormBoundarybLQwkp4opIEZn1fA..Acce
pt: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q
=0.8,image/png,*/*;q=0.5..Accept-Encoding: gzip,deflate,sdch..Accept-L
anguage: zh-CN,zh;q=0.8..Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3..Cook
ie: SESS3b0e658f87cf58240de13ab43a399df6=lju6o5bg8u04lv1ojugm2ccic6...
.
##
T 192.168.0.8:3981 -> 192.168.0.6:80 [AP]
------WebKitFormBoundarybLQwkp4opIEZn1fA..Content-Disposition: form-da
ta; name="n"....perfgeeks..------WebKitFormBoundarybLQwkp4opIEZn1fA..C
ontent-Disposition: form-data; name="f"; filename="test.txt"..Content-
Type: text/plain....i am file..multipart/form-data..------WebKitFormBo
undarybLQwkp4opIEZn1fA--..
##
從響應(yīng)輸出來比對,$_POST數(shù)據(jù)跟請求提交數(shù)據(jù)相符,即$_POST = array(‘n' => ‘perfgeeks')。這也跟http請求body中的數(shù)據(jù)相呼應(yīng),同時說明PHP把相應(yīng)的數(shù)據(jù)填入$_POST全局變量。而php://input 輸出為空,沒有輸出任何東西,盡管http請求數(shù)據(jù)包中body不為空。這表示,當(dāng)Content-Type為multipart/form-data的 時候,即便http請求body中存在數(shù)據(jù),php://input也為空,PHP此時,不會把數(shù)據(jù)填入php://input流。所以,可以確定: php://input不能用于讀取enctype=multipart/form-data數(shù)據(jù)。
我們再比較這次通過ngrep抓取的http請求數(shù)據(jù)包,我們會發(fā)現(xiàn),最大不同的一 點是Content-Type后面跟了boundary定義了數(shù)據(jù)的分界符,bounday是隨機生成的。另外一個大不一樣的,就是http entity body中的數(shù)據(jù)組織結(jié)構(gòu)不一樣了。
上一節(jié),我們概述了,當(dāng)Content-Type為application/x- www-form-urlencoded時,php://input和$_POST數(shù)據(jù)是“一致”的,為其它Content-Type的時候,php: //input和$_POST數(shù)據(jù)數(shù)據(jù)是不一致的。因為只有在Content-Type為application/x-www-form- urlencoded或者為multipart/form-data的時候,PHP才會將http請求數(shù)據(jù)包中的body相應(yīng)部分數(shù)據(jù)填入$_POST全 局變量中,其它情況PHP都忽略。而php://input除了在數(shù)據(jù)類型為multipart/form-data之外為空外,其它情況都可能不為空。 通過這一節(jié),我們更加明白了php://input與$_POST的區(qū)別與聯(lián)系。所以,再次確認,php://input無法讀取 enctype=multipart/form-data數(shù)據(jù),當(dāng)php://input遇到它時,永遠為空,即便http entity body有數(shù)據(jù)。
php://input VS $http_raw_post_data
相信大家對php://input已經(jīng)有一定深度地了解了。那 么$http_raw_post_data是什么呢?$http_raw_post_data是PHP內(nèi)置的一個全局變量。它用于,PHP在無法識別的 Content-Type的情況下,將POST過來的數(shù)據(jù)原樣地填入變量$http_raw_post_data。它同樣無法讀取Content- Type為multipart/form-data的POST數(shù)據(jù)。需要設(shè)置php.ini中的 always_populate_raw_post_data值為On,PHP才會總把POST數(shù)據(jù)填入變量$http_raw_post_data。
把腳本phpinput_server.php改變一下,可以驗證上述內(nèi)容 <?php
$raw_post_data = file_get_contents('php://input', 'r');
$rtn = ($raw_post_data == $HTTP_RAW_POST_DATA) ? 1 : 0;
echo $rtn;
?>
執(zhí)行測試腳本?
@php phpinput_post.php?
@php phpinput_get.php?
@php phpinput_xmlrpc.php?
得出的結(jié)果輸出都是一樣的,即都為1,表示php://input和$HTTP_RAW_POST_DATA是相同的。至于對內(nèi)存的壓力,我們這里就不做細致地測試了。有興趣的,可以通過xhprof進行測試和觀察。?
以此,我們這節(jié)可以總結(jié)如下:?
1, php://input 可以讀取http entity body中指定長度的值,由Content-Length指定長度,不管是POST方式或者GET方法提交過來的數(shù)據(jù)。但是,一般GET方法提交數(shù)據(jù) 時,http request entity body部分都為空。?
2,php://input 與$HTTP_RAW_POST_DATA讀取的數(shù)據(jù)是一樣的,都只讀取Content-Type不為multipart/form-data的數(shù)據(jù)。?
學(xué)習(xí)筆記?
1,Coentent-Type僅在取值為application/x-www-data-urlencoded和multipart/form-data兩種情況下,PHP才會將http請求數(shù)據(jù)包中相應(yīng)的數(shù)據(jù)填入全局變量$_POST?
2,PHP不能識別的Content-Type類型的時候,會將http請求包中相應(yīng)的數(shù)據(jù)填入變量$HTTP_RAW_POST_DATA?
3, 只有Coentent-Type不為multipart/form-data的時候,PHP不會將http請求數(shù)據(jù)包中的相應(yīng)數(shù)據(jù)填入php://input,否則其它情況都會。填入的長度,由Coentent-Length指定。?
4,只有Content-Type為application/x-www-data-urlencoded時,php://input數(shù)據(jù)才跟$_POST數(shù)據(jù)相一致。?
5,php://input數(shù)據(jù)總是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更湊效,且不需要特殊設(shè)置php.ini?
6,PHP會將PATH字段的query_path部分,填入全局變量$_GET。通常情況下,GET方法提交的http請求,body為空。
更多PHP輸入流php://input介紹相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Outils d'IA chauds

Undress AI Tool
Images de déshabillage gratuites

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Clothoff.io
Dissolvant de vêtements AI

Video Face Swap
échangez les visages dans n'importe quelle vidéo sans effort grace à notre outil d'échange de visage AI entièrement gratuit?!

Article chaud

Outils chauds

Bloc-notes++7.3.1
éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)