手册中摘取的几句话:

  1. 当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 时,会将变量以关联数组形式传入当前脚本。
  2. 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中以键值对的方式存放。

总结:

  1. 当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data :php://input 中是形同 a=1&b=2的原始数据。$_POST 中是关联数组,且没有上传控件的内容。
  2. php://input 是个可以访问请求的原始数据的只读流。 enctype="multipart/form-data" 的时候php://input 是无效的。
  3. $_POST 不能获取 Content-Type = "text/plain"时 post的数据, php://input可以。

随机推荐

  1. [CSP-S模拟测试]:炼金术士的疑惑(模拟+数学+高斯消元)

    题目传送门(内部题70) 输入格式 第一行一个正整数$n$,表示炼金术士已知的热化学方程式数量.接下来$n$行,每行一个炼金术士已知的热化学方程式.最后一行一个炼金术士想要求解的热化学方程式,末尾记为 ...

  2. Python 字典dict操作定义

    字典是用大括号{ }来表示,它是python中最灵活的内置数据类型.它是一个无序的集合,通过键来存取值,而不能用索引. 字典的创建和使用 字典的组成:字典是由大括号{  }来包含其数据的,大括号内包含 ...

  3. 建立起BI的支撑团队

    Bobby Luo 罗如意(18907295660@189.cn) 2011年7月 http://weibo.com/cquptvlry 电子商务中的BI应用初探 系统架构 对整个数据仓库的架构进行规 ...

  4. c# access oledb helper class

    连接Access数据库 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...

  5. 浅释Functor、Applicative与Monad

    引言 转入Scala一段时间以来,理解Functor.Applicative和Monad等概念,一直是我感到头疼的部分.虽然读过<Functors, Applicatives, And Mona ...

  6. kafka 和 rocketMQ 的数据存储

    kafka 版本:1.1.1 一个分区对应一个文件夹,数据以 segment 文件存储,segment 默认 1G. 分区文件夹: segment 文件: segment 的命名规则是怎样的? kaf ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_3_Collection集合常用功能

    Collection在java.util包下面 只学里面几个比较重要的,List和Set 一共7个共性方法 接口指向实现类,多态的形式. 输出这个结合打印出一个空的数组.说明它重写了toString的 ...

  8. Raudus入门(1)

    Raudus入门(1) (2013-08-09 14:38:17) 转载▼ 标签: it 分类: Delphi 基于delphi做web应用,有个Raudus,基于对ext js的封装,可以在delp ...

  9. Android Studio 连接夜神模拟器

    网上找到的解决是需要我们 然后运行cmd命令,cd到夜神安装目录,执行命令 nox_adb.exe connect 127.0.0.1:62001

  10. Cassandra commands

      Common commands:   describe keyspaces // 列出所有db use your_db; // 进去db describe tables; // 列出所有table ...