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

看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"的请求头:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值