手册中摘取的几句话:

  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. BBED ORA-00600: internal error code, arguments: [16703], [1403], [20], [], [], [], [], [], [], [], [], []

    BBED模拟并修复 删除:$ORACLE_HOME/rdbms/admin/prvtsupp.plb SQL> alter database open;alter database open*E ...

  2. qbzt day6 上午

    还是合并石子,但是这次可以任意两个合并,并且求最大异或和 f[s]表示把s所对应的的石子合并为一堆的最小代价 最后求f[2^n-1] 怎么转移? 最后一次也是把两堆合并成一堆,但是会有很多情况,可以枚 ...

  3. (转)SQLite部署-无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块

    本文转载自:http://www.cnblogs.com/muzhiye/p/4284070.html 近期刚使用SQLite,主要引用的是System.Data.SQLite.dll这个dll,在部 ...

  4. bp文件错误消除

    代码文件报错, error: unused parameter 'data' [-Werror,-Wunused-parameter]‘ 按提示在cflags中加入: "-Wunused-p ...

  5. Delphi XE2 之 FireMonkey 入门(6) - TLine、TEllipse、TCircle、TPie、TArc、TRectangle、TRoundRect、TCalloutRectangle

    它们都是继承自 TShape 类, 共同拥有如下属性: Fill            : TBrush;      //填充 Stroke          : TBrush;      //边线( ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第2节 匿名对象_6-匿名对象的说明

    没有名字的对象,叫做匿名对象 新建一个Person类 把赵又廷赋值交给匿名对象Person里面的成员变量name 想调用里面的ShowName的话 还需要再定义一个匿名对象. 第三个对象是一个全新的. ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_04 数据结构_3_数据结构_数组

    0x代表16进制的地址 arr通过首地址找到存储空间.

  8. altium学习之常用快捷键

    1.放大缩小:常用方法,ctrl+鼠标滚轮,鼠标中键+移动鼠标,pgup.pgup. 2.切换不同的布线层:ctrl+shift+鼠标滚轮 3.在SCH或者PCB 同一平面内左右翻转:ctrl+X 4 ...

  9. 【ABAP系列】SAP ABAP同时显示多个ALV的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP同时显示多个AL ...

  10. python字典-基础

    一.解释 像列表一样,“字典”是许多值的集合.但不像列表的下标,字典的索引可以 使用许多不同数据类型,不只是整数.字典的索引被称为“键”,键及其关联的值 称为“键-值”对. 二.列表创建方式 1. I ...