最新消息:

php中php://input的用法详细

php安全 admin 2592浏览 0评论
在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组。所以,这里主要探讨php输入流php://input。

下面的例子摘取的是wordpress中的一段代码,里面有用到http://input,有需要的可以进一步研究。

 代码如下 复制代码
if (!isset( $HTTP_RAW_POST_DATA ) ) {
$HTTP_RAW_POST_DATA = file_get_contents(‘php://input’);
}// fix for mozBlog and other cases where xml isn’t on the very first line
if ( isset($HTTP_RAW_POST_DATA) )
$HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);

对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数据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

 代码如下 复制代码
<?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”;
?>
?
<?php
//@file phpinput_post.php
$http_entity_body = ‘n=’ . urldecode(‘perfgeeks’) . ‘&amp;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.1rn”);
fputs($fp, “Host: {$host}rn”);
fputs($fp, “Content-Type: {$http_entity_type}rn”);
fputs($fp, “Content-Length: {$http_entity_length}rn”);
fputs($fp, “Connection: closernrn”);
fputs($fp, $http_entity_body . “rnrn”);
?
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}
?>

我们可以通过使用工具ngrep抓取http请求包(因为我们需要探知的是php://input,所以我们这里只抓取http Request数据包)。我们来执行测试脚本phpinput_post.php

 代码如下 复制代码
@php /phpinput_post.phpHTTP/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….

仔细观察,我们不难发现
1,$_POST数据,php://input 数据与httpd entity body数据是“一致”的
2,http请求中的Content-Type是application/x-www-form-urlencoded ,它表示http请求body中的数据是使用http的post方法提交的表单数据,并且进行了urlencode()处理。
(注:注意加粗部分内容,下文不再提示). http://www.k686.com

我们再来看看脚本phpinput_xmlrpc.php的原文件内容,它模拟了一个POST方法提交的xml-rpc请求。

 代码如下 复制代码
<?php
//@file phpinput_xmlrpc.php
$http_entity_body = “nn   jt_userinfon”;
$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.1rn”);
fputs($fp, “Host: {$host}rn”);
fputs($fp, “Content-Type: {$http_entity_type}rn”);
fputs($fp, “Content-Length: {$http_entity_length}rn”);
fputs($fp, “Connection: closernrn”);
fputs($fp, $http_entity_body . “rnrn”);
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
?
fclose($fp);
echo $d;
}
?>

同样地,让我们来执行这个测试脚本

 代码如下 复制代码
@php /phpinput_xmlrcp.phpHTTP/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————-
<?xml version=”1.0″>
<methodcall>
<name>jt_userinfo</name>
</methodcall>

执行这个脚本的时候,我们通过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….<?xml version=”1.0″>.<methodcall>.   <name>jt_userinfo<
/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。

 代码如下 复制代码
<?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’) . ‘&amp;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.1rn”);
fputs($fp, “Host: {$host}rn”);
fputs($fp, “Connection: closernrn”);
?
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}
?>

同样,我们执行下一phpinput_get.php测试脚本,它模拟了一个通常情况下的GET方法提交表单数据。

 代码如下 复制代码
@php /phpinput_get.phpHTTP/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数据。
实践中关于php://input用法总结:

1、只有Content-Type为application/x-www-data-urlencoded时,php://input数据才跟$_POST数据相一致。
2、PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量$HTTP_RAW_POST_DATA
3、只有Content-Type为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由Coentent-Length指定。
4、Content-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST。
5、php://input数据总是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini
6、PHP会将PATH字段的query_path部分,填入全局变量$_GET。php://input读取不到$_GET数据,是因为$_GET数据 作为query_path写在http请求头部(header)的PATH字段,而不是写在http请求的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。

php输入流php://input应用实例

用Content-Type=text/xml 类型,提交一个xml文档内容给了php server,要怎么获得这个POST数据。

The RAW / uninterpreted HTTP POST information can be accessed with:   $GLOBALS[‘HTTP_RAW_POST_DATA’] This is useful in cases where the post Content-Type is not something PHP understands (such as text/xml).

由于PHP默认只识别application/x-www-data-urlencoded和multipart/form-data标准的数据类型,因此,对型如text/xml的内容无法解析为$_POST数组,故保留原型,交给$GLOBALS[‘HTTP_RAW_POST_DATA’] 来接收。

另外还有一项 php://input 也可以实现此这个功能.

php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。php://input 不能用于 enctype=”multipart/form-data”

a.htm
——————
<form   action= “post.php ”   method= “post “>
<input   type= “text ”   name= “user “>
<input   type= “password ”   name= “password “>
<input   type= “submit “>
</form>

post.php
—————————-
<?   echo   file_get_contents( “php://input “);   ?>

 

转载请注明:jinglingshu的博客 » php中php://input的用法详细

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址