您好,欢迎访问一九零五行业门户网

PHP输入流 php://input与POST/GET的区别

php://input也是一种数据的提交方式了,但我们很少使用到php://input来做数据提交了,那么php://input与我们常用的post/get数据提交方式有什么区别呢,下面来看一篇文章。
php输入流php://input
在使用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”
我们应该怎么去理解这段概述呢?我把它划分为三部分,逐步去理解:
读取post数据
不能用于multipart/form-data类型
php://input vs $http_raw_post_data
读取post数据
phper们一定很熟悉$_post这个内置变量。$_post与php://input存在哪些关联与区别呢?另外,客户端向服务端交互数据,最常用的方法除了post之外,还有get。既然php://input作为php输入流,它能读取get数据吗?这二个问题正是我们这节需要探讨的主要内容。
经验告诉我们,从测试与观察中总结,会是一个很凑效的方法。这里,我写了几个脚本来帮助我们测试。
@file 192.168.0.6:/phpinput_server.php 打印出接收到的数据
@file 192.168.0.8:/phpinput_post.php 模拟以post方法提交表单数据
@file 192.168.0.8:/phpinput_xmlrpc.php 模拟以post方法发出xmlrpc请求.
@file 192.168.0.8:/phpinput_get.php 模拟以get方法提交表单表数
phpinput_server.php与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=' . urldecode('perfgeeks') . '&p=' . urldecode('7788');
$http_entity_type = 'application/x-www-form-urlencoded';
$http_entity_length = strlen($http_entity_body);
$host = '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.1\r\n);
  fputs($fp, host: {$host}\r\n);
  fputs($fp, content-type: {$http_entity_type}\r\n);
  fputs($fp, content-length: {$http_entity_length}\r\n);
  fputs($fp, connection: close\r\n\r\n);
  fputs($fp, $http_entity_body . \r\n\r\n);
while (!feof($fp)) {
    $d .= fgets($fp, 4096);
  }
  fclose($fp);
  echo $d;
}
我们可以通过使用工具ngrep抓取http请求包(因为我们需要探知的是php://input,所以我们这里只抓取http request数据包)。我们来执行测试脚本phpinput_post.php
@php /phpinput_post.php
http/1.1 200 ok
date: thu, 08 apr 2010 03:23:36 gmt
server: apache/2.2.3 (centos)
x-powered-by: php/5.1.6
content-length: 160
connection: 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
通过ngrep抓到的http请求包如下:
t 192.168.0.8:57846 -> 192.168.0.6:80 [ap]
  post /phpinput_server.php http/1.1..
  host: 192.168.0.6..content-type: application/x-www-form-urlencoded..co
  ntent-length: 18..connection: close....n=perfgeeks&p=7788....
仔细观察,我们不难发现:
$_post数据,php://input 数据与httpd entity body数据是“一致”的。
http请求中的content-type是application/x-www-form-urlencoded ,它表示http请求body中的数据是使用http的post方法提交的表单数据,并且进行了urlencode()处理。
(注:注意加粗部分内容,下文不再提示)。
我们再来看看脚本phpinput_xmlrpc.php的原文件内容,它模拟了一个post方法提交的xml-rpc请求。
//@file phpinput_xmlrpc.php
$http_entity_body = \n\n   jt_userinfo\n;
$http_entity_type = 'text/html';
$http_entity_length = strlen($http_entity_body);
$host = '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.1\r\n);
  fputs($fp, host: {$host}\r\n);
  fputs($fp, content-type: {$http_entity_type}\r\n);
  fputs($fp, content-length: {$http_entity_length}\r\n);
  fputs($fp, connection: close\r\n\r\n);
  fputs($fp, $http_entity_body . \r\n\r\n);
  while (!feof($fp)) {
    $d .= fgets($fp, 4096);
  }
fclose($fp);
  echo $d;
}
同样地,让我们来执行这个测试脚本:
@php /phpinput_xmlrcp.php
http/1.1 200 ok
date: thu, 08 apr 2010 03:47:18 gmt
server: apache/2.2.3 (centos)
x-powered-by: php/5.1.6
content-length: 154
connection: close
content-type: text/html; charset=utf-8
-------$_post------------------
array(0) {
}
-------php://input-------------
jt_userinfo
执行这个脚本的时候,我们通过ngrep抓取的http请求数据包如下:
t 192.168.0.8:45570 -> 192.168.0.6:80 [ap]
  post /phpinput_server.php http/1.1..
  host: 192.168.0.6..content-type: text/html..content-length: 75..connec
  tion: close......   jt_userinfo  /name>.....
同样,我样也可以很容易地发现:
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。
//@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;
//@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;
 }
同样,我们执行下一phpinput_get.php测试脚本,它模拟了一个通常情况下的get方法提交表单数据。
@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工具,捕获的相应的http请求数据包如下:
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。但是,如果强硬数据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
上传文件的时候,表单的写法是这样的:
那么,enctype=multipart/form-data这里的意义,就是将该次http请求头部(head)中的content-type设置为multipart/form-data。请查阅rfc1867对它的描述。multipart/form-data也表示以post方法提交表单数据,它还伴随了文件上传,所以会跟application/x-www-form-urlencoded数据格式不一样。它会以一更种更合理的,更高效的数据格式传递给服务端。我们提交该表单数据,并且打印出响应结果,如下:
-------$_post------------------
array(1) { [n]=> string(9) perfgeeks }
-------php://input-------------
同时,我们通过ngrep抓取的相应的http请求数据包如下:
########
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--..
##
从响应输出来比对,$_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改变一下,可以验证上述内容:
$raw_post_data = file_get_contents('php://input', 'r');
$rtn = ($raw_post_data == $http_raw_post_data) ? 1 : 0;
echo $rtn;
执行测试脚本:
@php phpinput_post.php
@php phpinput_get.php
@php phpinput_xmlrpc.php
得出的结果输出都是一样的,即都为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为空。
其它类似信息

推荐信息