What is the difference between PHP's $_post, $HTTP _raw_post_data and php://input in PHP?

Source: Internet
Author: User
Tags deprecated ini soap json

MAC Pro Computer compiled and installed PHP 5.6.21, the previous system runs the Times as follows warning level error:
deprecated:automatically populating $HTTP _raw_post_data is deprecated and would are removed in a future version. To avoid this warning set ' always_populate_raw_post_data ' to '-1 ' in php.ini and use the Php://input stream instead.

Automatic variable $HTTP _raw_post_data is obsolete and will be removed in the future and replaced with Php://input flow mode!


Summary: Distinguish PHP from $_post, $HTTP _raw_post_data and Php://input

1, HTML <form> enctype Attribute

All characters are encoded before application/x-www-form-urlencoded transfer (spaces is converted to +, special characters are converted to ASCII HEX)

Multipart/form-data no characters are encoded, generally with upload
Text/plain spaces is converted to +, but special characters are not 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
Name=jonathan+doe&age=23&formula=a+%2b+b+%3d%3d+13%25%21

2, $_post

$_post is the most common way to get a form, which organizes the submitted data in an associative array and encodes it, such as urldecode, or even coded conversions, which are recognized by the PHP default data type application/ x-www.form-urlencoded
Cannot parse content of a application/x-www.form-urlencoded data type such as Text/xml,application/json,soap

3. $HTTP _raw_post_data

The data type that the PHP default recognizes is application/x-www.form-urlencoded, with the Content-type=application/json type, the submitted POST data is not available at this time $_post, but using $GLOBALS [' http_raw_post_data '] can be obtained. Because when PHP does not recognize Content-type, the POST data is filled into the $HTTP _raw_post_data.
Setting the Always_populate_raw_post_data value in php.ini will not take effect
$HTTP _raw_post_data is empty when $_post can fetch a value
cannot be used for enctype= "Multipart/form-data"
This global variable has been removed from the PHP7, and the use of always_populate_raw_post_data can cause e_deprecated errors when the $HTTP _raw_post_data is populated. Use Php://input instead $HTTP _raw_post_data, because it may be removed in subsequent versions of PHP. Setting Always_populate_raw_post_data to-1 (this forces $HTTP _raw_post_data undefined and therefore does not cause e_deprecated errors) to experience new behavior.

4, Php://input

Php://input can obtain unprocessed post raw data through the input stream as a file read, allowing the original data of the post to be read. Compared with $HTTP _raw_post_data, it brings less pressure to memory.
No special php.ini settings are required
cannot be used for enctype= "Multipart/form-data"

Summarize

1, if the application/x-www-form-urlencoded and multipart/form-data formats are $_post;
2, if it is not available, such as Text/xml, application/ JSON, SOAP, using file_get_contents (' php://input ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.