手册中摘取的几句话:

  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. 前端学习之路之SPA(单页应用)设计原理

    SPA设计 1.设计意义 前后端分离 减轻服务器压力 增强用户体验 Prerender预渲染优化SEO 前后端分离:前端做业务逻辑,后端处理数据和接口,耦合度减少,开发效率提高. 减轻服务器压力:一个 ...

  2. Linux shell -查找字符(find,xargs,grep)

    在当前目录下查找含有jmxremote字符的文件 test@>find . -type f|xargs grep "jmxremote" . 当前目录 -type 查找文件类 ...

  3. an ordered dict within the ordered dict

    w http://stackoverflow.com/questions/20166749/how-to-convert-an-ordereddict-into-a-regular-dict-in-p ...

  4. npm 中的 --

    -- 命令告诉cli-parser停止向下解析, --后面的内容会传给命令行作为命令行参数 文档地址: https://docs.npmjs.com/misc/config.html

  5. 操作Redis--hash/key-value

    redis也是一个数据库,它的存储以key-value的方式存放,比如: a.关系型数据库 比如: mysql.oracle.sql server.db2.sqlite数据库,为关系型数据库 数据通过 ...

  6. 什么是HIS、PACS、LIS、RIS

    什么是HIS?医院信息系统的定义(HIS)医院信息系统(Hospital Information System,HIS)在国际学术界已公认为新兴的医学信息学(Medical Informatics)的 ...

  7. 在使用spring中的ContextConfiguration、test注解时出现的错误

    错误: 在使用测试注解时出现ContextConfiguration注解和test注解无法正常导包使用的编译异常,如图: 解决办法: 将pom.xml文件中以下依赖管理 中的<scope> ...

  8. python 链接交换机并执行相关命令

    原文地址 https://blog.csdn.net/u010897775/article/details/80311786?utm_source=blogxgwz0 # encoding=utf-8 ...

  9. HDU-4081.Qinshihuang'sNationalRoadSystem(次小生成树变种)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  10. AtCoder Beginner Contest 133 B - Good Distance

    地址:https://atcoder.jp/contests/abc133/tasks/abc133_b 核心问题:判断一个浮点数开方是否为整数 ; double ans1=sqrt(ans); if ...