Home  >  Article  >  Backend Development  >  php中php://input、$_POST和$HTTP_RAW_POST_DATA的异同

php中php://input、$_POST和$HTTP_RAW_POST_DATA的异同

WBOY
WBOYOriginal
2016-06-23 13:29:29895browse

在之前的工作中经常会遇见php://input这种写法,一直不知道为什么有了$_POST为什么还要用这种写法。这次总结一下。

先来看一个最简单的例子:

例一:

<?phpvar_dump (http_build_query($_POST));var_dump(file_get_contents("php://input"));var_dump($HTTP_RAW_POST_DATA);?>
年龄:

输入24后提交后输出结果为:


从例一我们可以得出以下结论:

1、通常情况下,http_build_query($_POST) =  file_get_contents("php://input"),但是Content-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST,而如果客户端向服务器post的数据是xml等格式时,$_POST是接收不了的,这种情况下只能用php://input或者是$HTTP_RAW_POST_DATA来接收。

2、$HTTP_RAW_POST_DATA依赖于php.ini文件中对于always_populate_raw_post_data参数的配置,如例一所示,在该参数设置为off的情况下,$HTTP_RAW_POST_DATA为null,其次,$HTTP_RAW_POST_DATA已在php5.6版本中废弃,官方建议使用php://input替代$HTTP_RAW_POST_DATA。使用php://input比激活 always_populate_raw_post_data 潜在需要更少的内存。


例二:

<?phpvar_dump (http_build_query($_POST));var_dump(file_get_contents("php://input"));var_dump($HTTP_RAW_POST_DATA);?>
年龄:
输入24后提交后输出结果为:


从例二可以看出:

当enctype="multipart/form-data" 的时候 php://input 是无效的。


以上就是php://input、$_POST和$HTTP_RAW_POST_DATA三者之间的主要区别。





版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn