Home >Backend Development >PHP Tutorial >【PHP】$_POST, $HTTP_RAW_POST_DATA, and php://input,httprawpostdata_PHP教程

【PHP】$_POST, $HTTP_RAW_POST_DATA, and php://input,httprawpostdata_PHP教程

WBOY
WBOYOriginal
2016-07-12 09:04:20873browse

[PHP]$_POST, $HTTP_RAW_POST_DATA, and php://input, httprawpostdata

1. HTML ff9c23ada1bcecdd1a0fb5d5a0f18437 enctype Attribute

  • application/x-www-form-urlencoded All characters will be encoded before transmission (spaces are converted to , special characters are converted to ASCII HEX)
  • multipart/form-data No characters are encoded, generally use upload
  • text/plain Spaces are converted to , but special characters will not be 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 is the most commonly used way to obtain a form. It organizes the submitted data in an associative array and performs encoding processing, such as urldecode, or even encoding conversion. The recognized data type is the data type recognized by PHP by default. application/x-www.form-urlencoded

Unable to parse content such as text/xml, application/json and other non-application/x-www.form-urlencoded data types

3. $HTTP_RAW_POST_DATA

The data type recognized by PHP by default is application/x-www.form-urlencoded, use Content-Type=application/json type, the submitted POST data $_POST cannot be obtained at this time, but use $GLOBALS['HTTP_RAW_POST_DATA '] can be obtained. Because when PHP cannot recognize the Content-Type, it will fill in the POST data into $HTTP_RAW_POST_DATA.

  • Set the always_populate_raw_post_data value in php.ini to On to take effect

  • When $_POST and php://input can get the value, $HTTP_RAW_POST_DATA is empty

  • cannot be used with enctype="multipart/form-data"

  • This global variable has been removed in PHP7 and replaced with php://input. Using always_populate_raw_post_data will cause errors when filling in $HTTP_RAW_POST_DATAE_DEPRECATED. Please use php://input instead of $HTTP_RAW_POST_DATA as it may be removed in subsequent PHP versions. Set always_populate_raw_post_data to -1 (this will force $HTTP_RAW_POST_DATA to be undefined, so it will not cause E_DEPRECATED errors) to experience the new behavior.

4. php://input

php://input can obtain unprocessed POST raw data through file reading through the input stream, allowing reading of POST raw data. It puts less pressure on memory than $HTTP_RAW_POST_DATA.

  • No special php.ini settings required

  • cannot be used with enctype="multipart/form-data"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1073810.htmlTechArticle[PHP]$_POST, $HTTP_RAW_POST_DATA, and php://input, httprawpostdata 1. HTML form enctype Attribute application /x-www-form-urlencoded All characters before transmission will be encoded...
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