$_post in PHP, $HTTP the difference between _raw_post_data and Php://input

Source: Internet
Author: User
Tags form post ini ascii hex values

HTML <form< Tag Enctype properties

First, to understand what is the enctype attribute of the <form< tag, the enctype attribute sets out how to encode the form data before it is sent to the server, with three different types:

Attribute Value Description
application/x-www-form-urlencoded default mode, all characters are encoded before they are sent to the server (spaces are converted to "+" plus, special symbols are converted to ASCII HEX values)
Multipart/form-data does not encode characters, you must use this value when you use a form that contains file upload controls.
Text/plain spaces are converted to "+" plus, but no special characters are encoded.
When you submit a form to a Web page, you can use three PHP methods to get Post data: $_post, $HTTP _raw_post_data,and Php://input, what's the difference?

$_post

$_post is the most common way to get the form POST data (the body part), and the uploaded file information is obtained using $_files.

$HTTP _raw_post_data

When the browser sends a POST request from the form, the default media type is "application/x-www-form-urlencoded", meaning that the field name and value are encoded and each key-value is separated by the ' & ' character. Key and value are separated by ' = ', and the spaces in key and value are replaced with +, and other special characters are encoded using the UrlEncode method.

For example, the following key-value:

Name:jonathan Doe
Age:23
Formula:a + b = 13%!
will be encoded with the following raw data:

Name=jonathan+doe&age=23&formula=a+%2b+b+%3d%3d+13%25%21
PHP parses the original POST data and formats the array to populate the $_post:

Array
(
[Name] => Jonathan Doe
[Age] => 23
[Formula] => A + b = 13%!
)
$HTTP _raw_post_data is a predefined variable in PHP that is used to get the original POST data, such as in the case above, the value of the $HTTP _raw_post_data is:

Name=jonathan+doe&age=23&formula=a+%2b+b+%3d%3d+13%25%21
But $HTTP _raw_post_data need to be set up in php.ini:

Always_populate_raw_post_data = On
Also, $HTTP _raw_post_data does not support the enctype= "Multipart/form-data" Way of data, in which case, we want to use $_post to get the contents of the field, $_files to obtain uploaded file information.

Php://input

Since $HTTP _raw_post_data depends on Pho.ini settings, is there a better way?

We can use Php://input to get the original POST data, and php://input consumes less memory than the $HTTP _raw_post_data, of course php://input and $HTTP _raw_post_data Holding enctype= "Multipart/form-data" way of data transmission.

Since Php://input is just a stream of data, we can use the file_get_contents () function to get its contents:

$post _data = file_get_contents (' php://input ');
Print_r ($post _data);
The content you get is the same as $HTTP _raw_post_data.

What is the use of the original POST data?

So what's the use of the original POST data? Because most of the time, we receive the data that is not the post of the page, but the data in the "Text/xml" format that can be posted by other means, which cannot be parsed into a $_post array, we need the original post data for processing.

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.