在使用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
<div class="codesnip-container"><pre class="php codesnip"><span class="kw2"><strong><?php</strong></span><span class="co1"><span style="color: #808080;">[email protected] phpinput_server.php</span></span><span class="re0">$raw_post_data</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">file_get_contents</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="st_h">'php://input'</span><span class="sy0">,</span> <span class="st_h">'r'</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="st0"><span style="color: #ff0000;">"-------<span class="es1">\$</span>_POST------------------<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="kw3"><span style="color: #000066;">var_dump</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$_POST</span><span class="br0"><span style="color: #66cc66;">)</span></span> <span class="sy0">.</span> <span class="st0"><span style="color: #ff0000;">"<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="st0"><span style="color: #ff0000;">"-------php://input-------------<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="re0">$raw_post_data</span> <span class="sy0">.</span> <span class="st0"><span style="color: #ff0000;">"<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="sy1">?></span>?<span class="kw2"><strong><?php</strong></span><span class="co1"><span style="color: #808080;">[email protected] phpinput_post.php</span></span><span class="re0">$http_entity_body</span> <span class="sy0">=</span> <span class="st_h">'n='</span> <span class="sy0">.</span> <span class="kw3"><span style="color: #000066;">urldecode</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="st_h">'perfgeeks'</span><span class="br0"><span style="color: #66cc66;">)</span></span> <span class="sy0">.</span> <span class="st_h">'&p='</span> <span class="sy0">.</span> <span class="kw3"><span style="color: #000066;">urldecode</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="st_h">'7788'</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="re0">$http_entity_type</span> <span class="sy0">=</span> <span class="st_h">'application/x-www-form-urlencoded'</span><span class="sy0">;</span><span class="re0">$http_entity_length</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">strlen</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$http_entity_body</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="re0">$host</span> <span class="sy0">=</span> <span class="st_h">'192.168.0.6'</span><span class="sy0">;</span><span class="re0">$port</span> <span class="sy0">=</span> <span class="nu0"><span style="color: #ff33ff;">80</span></span><span class="sy0">;</span><span class="re0">$path</span> <span class="sy0">=</span> <span class="st_h">'/phpinput_server.php'</span><span class="sy0">;</span><span class="re0">$fp</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">fsockopen</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$host</span><span class="sy0">,</span> <span class="re0">$port</span><span class="sy0">,</span> <span class="re0">$error_no</span><span class="sy0">,</span> <span class="re0">$error_desc</span><span class="sy0">,</span> 30<span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">if</span></span> <span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="br0"><span style="color: #66cc66;">)</span></span> <span class="br0"><span style="color: #66cc66;">{</span></span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"POST <span class="es4">{$path}</span> HTTP/1.1<span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Host: <span class="es4">{$host}</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Content-Type: <span class="es4">{$http_entity_type}</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Content-Length: <span class="es4">{$http_entity_length}</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Connection: close<span class="es1">\r</span><span class="es1">\n</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="re0">$http_entity_body</span> <span class="sy0">.</span> <span class="st0"><span style="color: #ff0000;">"<span class="es1">\r</span><span class="es1">\n</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span>? <span class="kw1"><span style="color: #a1a100;">while</span></span> <span class="br0"><span style="color: #66cc66;">(</span></span><span class="sy0">!</span><span class="kw3"><span style="color: #000066;">feof</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span style="color: #66cc66;"><span class="br0">)</span><span class="br0">)</span></span> <span class="br0"><span style="color: #66cc66;">{</span></span> <span class="re0">$d</span> <span class="sy0">.=</span> <span class="kw3"><span style="color: #000066;">fgets</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> 4096<span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="br0"><span style="color: #66cc66;">}</span></span> <span class="kw3"><span style="color: #000066;">fclose</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="re0">$d</span><span class="sy0">;</span><span class="br0"><span style="color: #66cc66;">}</span></span><span class="sy1">?></span>
我们可以通过使用工具ngrep抓取http请求包(因为我们需要探知的是php://input,所以我们这里只抓取http Request数据包)。我们来执行测试脚本phpinput_post.php
<span class="sy0">@</span>php <span class="sy0">/</span>phpinput_post.php
HTTP/1.1 200 OKDate: Thu, 08 Apr 2010 03:23:36 GMTServer: Apache/2.2.3 (CentOS)X-Powered-By: PHP/5.1.6Content-Length: 160Connection: closeContent-Type: text/html; charset=UTF-8-------$_POST------------------array(2) { ["n"]=> string(9) "perfgeeks" ["p"]=> string(4) "7788"}-------php://input-------------<strong>n=perfgeeks&p=7788</strong>
通过ngrep抓到的http请求包如下:
T 192.168.0.8:57846 -> 192.168.0.6:80 [AP] <strong>POST</strong> /phpinput_server.php HTTP/1.1.. Host: 192.168.0.6..<strong>Content-Type: application/x-www-form-urlencoded</strong>..Co ntent-Length: 18..Connection: close....<strong>n=perfgeeks&p=7788</strong>....
仔细观察,我们不难发现
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请求。
<div class="codesnip-container"><pre class="php codesnip"><span class="kw2"><strong><?php</strong></span><span class="co1"><span style="color: #808080;">[email protected] phpinput_xmlrpc.php</span></span><span class="re0">$http_entity_body</span> <span class="sy0">=</span> <span class="st0"><span style="color: #ff0000;">"<span class="es1">\n</span><span class="es1">\n</span> jt_userinfo<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="re0">$http_entity_type</span> <span class="sy0">=</span> <span class="st_h">'text/html'</span><span class="sy0">;</span><span class="re0">$http_entity_length</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">strlen</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$http_entity_body</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="re0">$host</span> <span class="sy0">=</span> <span class="st_h">'192.168.0.6'</span><span class="sy0">;</span><span class="re0">$port</span> <span class="sy0">=</span> <span class="nu0"><span style="color: #ff33ff;">80</span></span><span class="sy0">;</span><span class="re0">$path</span> <span class="sy0">=</span> <span class="st_h">'/phpinput_server.php'</span><span class="sy0">;</span><span class="re0">$fp</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">fsockopen</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$host</span><span class="sy0">,</span> <span class="re0">$port</span><span class="sy0">,</span> <span class="re0">$error_no</span><span class="sy0">,</span> <span class="re0">$error_desc</span><span class="sy0">,</span> 30<span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">if</span></span> <span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="br0"><span style="color: #66cc66;">)</span></span> <span class="br0"><span style="color: #66cc66;">{</span></span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"POST <span class="es4">{$path}</span> HTTP/1.1<span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Host: <span class="es4">{$host}</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Content-Type: <span class="es4">{$http_entity_type}</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Content-Length: <span class="es4">{$http_entity_length}</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Connection: close<span class="es1">\r</span><span class="es1">\n</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="re0">$http_entity_body</span> <span class="sy0">.</span> <span class="st0"><span style="color: #ff0000;">"<span class="es1">\r</span><span class="es1">\n</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw1"><span style="color: #a1a100;">while</span></span> <span class="br0"><span style="color: #66cc66;">(</span></span><span class="sy0">!</span><span class="kw3"><span style="color: #000066;">feof</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span style="color: #66cc66;"><span class="br0">)</span><span class="br0">)</span></span> <span class="br0"><span style="color: #66cc66;">{</span></span> <span class="re0">$d</span> <span class="sy0">.=</span> <span class="kw3"><span style="color: #000066;">fgets</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> 4096<span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="br0"><span style="color: #66cc66;">}</span></span>? <span class="kw3"><span style="color: #000066;">fclose</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="re0">$d</span><span class="sy0">;</span><span class="br0"><span style="color: #66cc66;">}</span></span><span class="sy1">?></span>
同样地,让我们来执行这个测试脚本
<span class="sy0">@</span>php <span class="sy0">/</span>phpinput_xmlrcp.php
HTTP/1.1 200 OKDate: Thu, 08 Apr 2010 03:47:18 GMTServer: Apache/2.2.3 (CentOS)X-Powered-By: PHP/5.1.6Content-Length: 154Connection: closeContent-Type: text/html; charset=UTF-8-------$_POST------------------array(0) {}-------php://input-------------<strong><?xml version="1.0"><methodcall> <name>jt_userinfo</name></methodcall></strong>
执行这个脚本的时候,我们通过ngrep抓取的http请求数据包如下
T 192.168.0.8:45570 -> 192.168.0.6:80 [AP] <strong>POST</strong> /phpinput_server.php HTTP/1.1.. Host: 192.168.0.6..<strong>Content-Type: text/html</strong>..Content-Length: 75..Connec tion: close....<strong><?xml version="1.0">.<methodcall>. <name>jt_userinfo< /name>.</methodcall></strong>....
同样,我样也可以很容易地发现:
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。
<div class="codesnip-container"><pre class="php codesnip"><span class="kw2"><strong><?php</strong></span><span class="co1"><span style="color: #808080;">[email protected] phpinput_server.php</span></span><span class="re0">$raw_post_data</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">file_get_contents</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="st_h">'php://input'</span><span class="sy0">,</span> <span class="st_h">'r'</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="st0"><span style="color: #ff0000;">"-------<span class="es1">\$</span>_GET------------------<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="kw3"><span style="color: #000066;">var_dump</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$_GET</span><span class="br0"><span style="color: #66cc66;">)</span></span> <span class="sy0">.</span> <span class="st0"><span style="color: #ff0000;">"<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="st0"><span style="color: #ff0000;">"-------php://input-------------<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="re0">$raw_post_data</span> <span class="sy0">.</span> <span class="st0"><span style="color: #ff0000;">"<span class="es1">\n</span>"</span></span><span class="sy0">;</span><span class="sy1">?></span>?<span class="kw2"><strong><?php</strong></span><span class="co1"><span style="color: #808080;">[email protected] phpinput_get.php</span></span><span class="re0">$query_path</span> <span class="sy0">=</span> <span class="st_h">'n='</span> <span class="sy0">.</span> <span class="kw3"><span style="color: #000066;">urldecode</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="st_h">'perfgeeks'</span><span class="br0"><span style="color: #66cc66;">)</span></span> <span class="sy0">.</span> <span class="st_h">'&p='</span> <span class="sy0">.</span> <span class="kw3"><span style="color: #000066;">urldecode</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="st_h">'7788'</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="re0">$host</span> <span class="sy0">=</span> <span class="st_h">'192.168.0.6'</span><span class="sy0">;</span><span class="re0">$port</span> <span class="sy0">=</span> <span class="nu0"><span style="color: #ff33ff;">80</span></span><span class="sy0">;</span><span class="re0">$path</span> <span class="sy0">=</span> <span class="st_h">'/phpinput_server.php'</span><span class="sy0">;</span><span class="re0">$d</span> <span class="sy0">=</span> <span class="st_h">''</span><span class="sy0">;</span><span class="re0">$fp</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">fsockopen</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$host</span><span class="sy0">,</span> <span class="re0">$port</span><span class="sy0">,</span> <span class="re0">$error_no</span><span class="sy0">,</span> <span class="re0">$error_desc</span><span class="sy0">,</span> 30<span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">if</span></span> <span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="br0"><span style="color: #66cc66;">)</span></span> <span class="br0"><span style="color: #66cc66;">{</span></span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"GET <span class="es4">{$path}</span>?<span class="es4">{$query_path}</span> HTTP/1.1<span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Host: <span class="es4">{$host}</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw3"><span style="color: #000066;">fputs</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> <span class="st0"><span style="color: #ff0000;">"Connection: close<span class="es1">\r</span><span class="es1">\n</span><span class="es1">\r</span><span class="es1">\n</span>"</span></span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span>? <span class="kw1"><span style="color: #a1a100;">while</span></span> <span class="br0"><span style="color: #66cc66;">(</span></span><span class="sy0">!</span><span class="kw3"><span style="color: #000066;">feof</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span style="color: #66cc66;"><span class="br0">)</span><span class="br0">)</span></span> <span class="br0"><span style="color: #66cc66;">{</span></span> <span class="re0">$d</span> <span class="sy0">.=</span> <span class="kw3"><span style="color: #000066;">fgets</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="sy0">,</span> 4096<span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="br0"><span style="color: #66cc66;">}</span></span> <span class="kw3"><span style="color: #000066;">fclose</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$fp</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span> <span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="re0">$d</span><span class="sy0">;</span> <span class="br0"><span style="color: #66cc66;">}</span></span><span class="sy1">?></span>
同样,我们执行下一phpinput_get.php测试脚本,它模拟了一个通常情况下的GET方法提交表单数据。
<span class="sy0">@</span>php <span class="sy0">/</span>phpinput_get.php
HTTP/1.1 200 OKDate: Thu, 08 Apr 2010 07:38:15 GMTServer: Apache/2.2.3 (CentOS)X-Powered-By: PHP/5.1.6Content-Length: 141Connection: closeContent-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] <strong>GET</strong> /phpinput_server.php?<strong>n=perfgeeks&p=7788</strong> 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数据。
所根据,上面几个探测,我们可以作出以下总结:
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
上传文件的时候,表单的写法是这样的
<div class="codesnip-container"><pre class="html4strict codesnip"><span class="sc2"><<span class="kw2"><strong><span style="color: #000000;">form</span></strong></span> <span class="kw3"><span style="color: #000066;">enctype</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"multipart/form-data"</span></span> <span class="kw3"><span style="color: #000066;">action</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"phpinput_server.php"</span></span> <span class="kw3"><span style="color: #000066;">method</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"POST"</span></span> ></span> <span class="sc2"><<span class="kw2"><strong><span style="color: #000000;">input</span></strong></span> <span class="kw3"><span style="color: #000066;">type</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"text"</span></span> <span class="kw3"><span style="color: #000066;">name</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"n"</span></span> <span class="sy0">/</span>></span> <span class="sc2"><<span class="kw2"><strong><span style="color: #000000;">input</span></strong></span> <span class="kw3"><span style="color: #000066;">type</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"file"</span></span> <span class="kw3"><span style="color: #000066;">name</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"f"</span></span> <span class="sy0">/</span>></span> <span class="sc2"><<span class="kw2"><strong><span style="color: #000000;">input</span></strong></span> <span class="kw3"><span style="color: #000066;">type</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"submit"</span></span> <span class="kw3"><span style="color: #000066;">value</span></span><span class="sy0">=</span><span class="st0"><span style="color: #ff0000;">"upload now"</span></span> <span class="sy0">/</span>></span><span class="sc2"><<span class="sy0">/</span><span class="kw2"><strong><span style="color: #000000;">form</span></strong></span>></span>
那么,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] <strong> POST</strong> /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..<strong>Content-Type: mult ipart/form-data; boundary=----WebKitFormBoundarybLQwkp4opIEZn1fA</strong>..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; <strong>name="n"....perfgeeks</strong>..------WebKitFormBoundarybLQwkp4opIEZn1fA..C ontent-Disposition: form-data; <strong>name="f"; filename="test.txt"..Content- Type: text/plain....i am file</strong>..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改变一下,可以验证上述内容
<div class="codesnip-container"><pre class="php codesnip"><span class="kw2"><strong><?php</strong></span><span class="re0">$raw_post_data</span> <span class="sy0">=</span> <span class="kw3"><span style="color: #000066;">file_get_contents</span></span><span class="br0"><span style="color: #66cc66;">(</span></span><span class="st_h">'php://input'</span><span class="sy0">,</span> <span class="st_h">'r'</span><span class="br0"><span style="color: #66cc66;">)</span></span><span class="sy0">;</span><span class="re0">$rtn</span> <span class="sy0">=</span> <span class="br0"><span style="color: #66cc66;">(</span></span><span class="re0">$raw_post_data</span> <span class="sy0">==</span> <span class="re0">$HTTP_RAW_POST_DATA</span><span class="br0"><span style="color: #66cc66;">)</span></span> ? 1 <span class="sy0">:</span> <span class="nu0"><span style="color: #ff33ff;">0</span></span><span class="sy0">;</span><span class="kw1"><span style="color: #a1a100;">echo</span></span> <span class="re0">$rtn</span><span class="sy0">;</span><span class="sy1">?></span>
执行测试脚本
<span class="sy0">@</span>php phpinput_post.php<span class="sy0">@</span>php phpinput_get.php<span class="sy0">@</span>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为空。
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31
- So öffnen Sie eine PHP-Datei
- Der Unterschied zwischen get und post
- Der Unterschied zwischen Get-Request und Post-Request
- So entfernen Sie die ersten paar Elemente eines Arrays in PHP
- Was tun, wenn die PHP-Deserialisierung fehlschlägt?
- So verbinden Sie PHP mit der MSSQL-Datenbank
- So verbinden Sie PHP mit der MSSQL-Datenbank
- So laden Sie HTML hoch