$_POST 和 php://input 的区别
手册中摘取的几句话:
- 当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 时,会将变量以关联数组形式传入当前脚本。
- php://input 是个可以访问请求的原始数据的只读流。 enctype="multipart/form-data" 的时候php://input 是无效的。
验证下:
post.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="getpost.php" method="post">
<input type="text" name="name" value="saisai">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
getpost.php
<?php
echo "----------input--------<br />";
var_dump(file_get_contents('php://input', 'r'));
echo "----------post---------<br />";
var_dump($_POST);
?>
一、enctype="application/x-www-form-urlencoded"
请求主体:
Content-Type: application/x-www-form-urlencoded
Content-Length: 25 name=saisai&submit=submit
输出:
----------input-------- string 'name=saisai&submit=submit' (length=25) ----------post--------- array (size=2)
'name' => string 'saisai' (length=6)
'submit' => string 'submit' (length=6)
小结:当enctype="application/x-www-form-urlencoded"时,请求主体(request body)中的数据(name=saisai&submit=submit)转换成关联数组放入$_POST,而 php://input 则获取的是原始数据(raw data)。
二、enctype=“multipart/form-data”时
2.1 表单:
<form action="getpost.php" method="post" enctype="multipart/form-data">
<input type="text" name="name" value="saisai">
<input type="submit" name="submit" value="submit">
</form> 请求主题:
Content-Type: multipart/form-data; boundary=---------------------------22554656810024
Content-Length: 249 -----------------------------22554656810024
Content-Disposition: form-data; name="name" saisai
-----------------------------22554656810024
Content-Disposition: form-data; name="submit" submit
-----------------------------22554656810024--
输出:
----------input-------- string '' (length=0) ----------post--------- array (size=2)
'name' => string 'saisai' (length=6)
'submit' => string 'submit' (length=6)
小结:在enctype="multipart/form-data" 且没有上传文件控件时,$_POST 能正常打印数据,php:// 无效。
2.2 表单(添加一个文件上传):
<form action="getpost.php" method="post" enctype="multipart/form-data">
<input type="text" name="name" value="saisai">
<input type="submit" name="submit" value="submit">
</form>
请求主题:
Content-Type: multipart/form-data; boundary=---------------------------272321281228527
Content-Length: 68386 -----------------------------272321281228527
Content-Disposition: form-data; name="name" saisai
-----------------------------272321281228527
Content-Disposition: form-data; name="filename"; filename="dog.png"
Content-Type: image/png 一堆乱码
-----------------------------272321281228527
Content-Disposition: form-data; name="submit" submit
-----------------------------272321281228527--
输出:
----------input-------- string '' (length=0) ----------post--------- array (size=2)
'name' => string 'saisai' (length=6)
'submit' => string 'submit' (length=6)
小结:在enctype="multipart/form-data" 且有上传文件控件时,$_POST 能打印出传入的数据,但是排除了上传的任何内容。php:// 无效。
三、enctype="text/plain"
表单:
<form action="getpost.php" method="post" enctype="text/plain">
<input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit">
</form>
请求主体:
Content-Type: text/plain
Content-Length: 28 name=saisai
submit=submit
输出:
----------input-------- string 'name=saisai submit=submit ' (length=28) ----------post--------- array (size=0)
empty
小结:enctype="text/plain"时,$_POST中没有内容,php://input中以键值对的方式存放。
总结:
- 当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data :php://input 中是形同 a=1&b=2的原始数据。$_POST 中是关联数组,且没有上传控件的内容。
- php://input 是个可以访问请求的原始数据的只读流。 enctype="multipart/form-data" 的时候php://input 是无效的。
- $_POST 不能获取 Content-Type = "text/plain"时 post的数据, php://input可以。
随机推荐
- spring bean.xml
http://blog.csdn.net/lanshengsheng2012/article/details/9011635
- Vertical Center TextView . 竖直居中的UITextView
@interface VerticalCenterTextView : UITextView @end @implementation VerticalCenterTextView - (void) ...
- java SimpleDateFormat setLenient用法
参考博客:https://www.cnblogs.com/my-king/p/4276577.html SimpleDateFormat.setLenient(true) : 默认值true,不严格解 ...
- Tomcat/weblogic session失效时间的几种设置方法
一.在容器中设置tomcat中配置server.xml中定义context时采用如下定义: <Context path="/livsorder" docBase=" ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_6_练习_转换文件编码
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_3_Map接口中的常用方法
这个方法比较特殊,它的返回值是V他也就是Vlaue get remove containsKey: put value没有重复的所以v1返回的是null key值有重复,所以会返回被替换的值,范冰冰1 ...
- rac的一次问题 ORA-01565: error in identifying file '+DATA/bol/spfilebol.ora'
昨天安装的测试环境的rac--2节点 CentOS release 6.8 (Final) SQL*Plus: Release 11.2.0.4.0 Production 今天测试突然出现问题 在ra ...
- 如何在idea中查看jar包源码
文章目录 准备jar包 idea打开文件夹 最后一步 准备jar包 例如,我准备看resin的jar,在桌面准备了一份 idea打开文件夹 在idea中file====>open=====> ...
- node和数据库建立连接
var express = require('express') , app = express(); var querystring = require('querystring'); var ut ...
- 编程字体Source Code Pro 免费下载
对于程序员来说,好的字体应该满足的基本条件: 字母和数字易于分辨,如: 英文字母o 和 阿拉伯数字 0 ,或者 英文字母 l 和 阿拉伯数字 1 ,两个单引号 '' 和双引号 ”. 字体等宽,保持对齐 ...