您的位置:首页 > 理论基础 > 计算机网络

php://input 和$_POST和$GLOBALS['HTTP_RAW_POST_DATA']的简单区别

2015-07-03 11:33 791 查看
看tp的源码,I函数看到这么一句:

case 'get' :
$input =& $_GET;
break;
case 'post' :
$input =& $_POST;
break;
case 'put' :
if(is_null($_PUT)){
parse_str(file_get_contents('php://input'), $_PUT);
}
$input = $_PUT;
break;

这个input是个什么鬼?
自己做了小实验:

首先一个表单,有三种方式get post 和一个enctype="multipart/form-data文件上传属性,代码简单:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
</head>
<body>
<!-- <form action="./input.php" method="POST" enctype="multipart/form-data"> -->
<!-- <form action="./input.php" method="POST" > -->
<form action="./input.php" method="GET" >

<input type="text" name='name' />
<input type="password" name='password' />
<input type="submit" />
</form>
</body>
</html>

然后一个input.php接收:
<?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”

比HTTP_RAW_POST_DATA占用更小内存,不需要在ini中设置
*/
var_dump($_POST);

echo '<hr>';

$raw_post_data = file_get_contents('php://input', 'r');

var_dump($raw_post_data);

echo '<hr>';
// 需要设置 always_populate_raw_post_data 为on ,在php.ini 中,前面的分号去掉即可
var_dump($GLOBALS['HTTP_RAW_POST_DATA']);

/*
结论:
1.php://input 无法读取到 表单类型为 enctype=multipart/form-data的数据
2.而$GLOBALS['HTTP_RAW_POST_DATA']在PHP在无法识别的 Content-Type的情况下,将POST过来的数据原样地填入变量$http_raw_post_data,但是也无法读取到Content- Type为multipart/form-data的POST数据。
3.GET的数据他们三个(POST,INPUT ,HTTP_RAW_POST_DATA都拿不到)

*/

结论写在注释里了,配上四幅图:
第一个图:

method为input,并且phpini中开启了always_populate_raw_post_data 为on



可以看到三者完全一样。

第二幅图,看看他的请求头:



第三张图,是表单中加入了enctype="multipart/form-data"属性,



再看一下上面加入enctype="multipart/form-data"的请求头:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息