search
Homephp教程php手册【PHP】$_POST, $HTTP_RAW_POST_DATA, and php://input,httprawpostdata

【PHP】$_POST, $HTTP_RAW_POST_DATA, and php://input,httprawpostdata

1、HTML

enctype Attribute
  • application/x-www-form-urlencoded  传送之前所有的字符都会被encoded,(spaces 被转换为+、特殊字符被转换为ASCII HEX)
  • multipart/form-data  没有字符被encoded,一般用upload
  • text/plain Spaces被转换为 + ,但是特殊字符不会被encoded

For example, the key-value pairs

name: Jonathan Doe
age: 23
formula: a + b == 13%!

are encoded as the following raw data:

name=Jonathan+Doe&age=23&formula=a+%2B+b+%3D%3D+13%25%21

$_POST

Array
(
    [name] => Jonathan Doe
    [age] => 23
    [formula] => a + b == 13%!
)

$HTTP_RAW_POST_DATA

print_r($GLOBALS['HTTP_RAW_POST_DATA'] );
name=Jonathan+Doe&age=23&formula=a+%2B+b+%3D%3D+13%25%21

php://input

$post_data = file_get_contents('php://input');
print_r($post_data);

name=Jonathan+Doe&age=23&formula=a+%2B+b+%3D%3D+13%25%21

2、$_POST

$_POST是最常用的获取表单的方式,它是以关联数组方式组织提交的数据,并对此进行编码处理,如urldecode,甚至编码转换,识别的数据类型是PHP默认识别的数据类型 application/x-www.form-urlencoded

无法解析如text/xml,application/json等非 application/x-www.form-urlencoded 数据类型的内容

3、$HTTP_RAW_POST_DATA

PHP默认识别的数据类型是application/x-www.form-urlencoded,用Content-Type=application/json 类型,提交的POST数据这时候 $_POST 就无法获取到了,但是使用 $GLOBALS['HTTP_RAW_POST_DATA'] 可以获取到。因为在PHP无法识别Content-Type的时候,就会把 POST 数据填入到 $HTTP_RAW_POST_DATA 中。

  • 设置 php.ini 中的 always_populate_raw_post_data 值为 On 才会生效

  • 当$_POST 与 php://input可以取到值时 $HTTP_RAW_POST_DATA 为空

  • 不能用于 enctype="multipart/form-data"

  • PHP7中已经移除了这个全局变量,用 php://input 替代,使用 always_populate_raw_post_data 会导致在填充 $HTTP_RAW_POST_DATA 时产生 E_DEPRECATED 错误。 请使用php://input 替代 $HTTP_RAW_POST_DATA, 因为它可能在后续的 PHP 版本中被移除。 设置always_populate_raw_post_data 为 -1 (这样会强制 $HTTP_RAW_POST_DATA 未定义,所以也不回导致 E_DEPRECATED的错误) 来体验新的行为。

4、php://input

php://input 可通过输入流以文件读取方式取得未经处理的POST原始数据,允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小。

  • 不需要任何特殊的 php.ini 设置

  • 不能用于 enctype="multipart/form-data"

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!