PHP three ways to get post data

Source: Internet
Author: User
This article introduces three ways to get POST data from PHP, the first method is $_post, the second method is to use File_get_contents, the third method is to use the global variable $globals, see below for details

Three ways to get post data from PHP

Method One, $_post

$_post or $_request stores the data in PHP in the form of a key=>value format.

Method two, using file_get_contents ("Php://input")

For post data that does not specify Content-type, you can use File_get_contents ("Php://input") to get the raw data. In fact, this method is used for any data received by the post in PHP. Without considering Content-type, including binary file streams is also possible.

Compared to $http_raw_post_data, it brings less pressure to memory and does not require any special php.ini settings.

Php://input cannot read the post data content-type is multipart/form-data, you need to set the Always_populate_raw_post_data value in php.ini to ON.

The php://input cannot read $_get data. This is because the $_get data is written as Query_path in the Path field of the HTTP request header (header) instead of the body part of the HTTP request.

Method Three, use global variable $globals[' http_raw_post_data ']

The $globals[' Http_raw_post_data ' store is the raw data that is sent to the POST.

But whether or not to save the POST data in $globals[' Http_raw_post_data '] depends on the settings of Centent-type, only if PHP is in an unrecognized content-type case. The POST data will be filled in the variable $globals[' http_raw_post_data '] as it is, and the variable is empty like content-type=application/x-www-form-urlencoded.

In addition, it is also unable to read the post data of Content-type to Multipart/form-data, but also to set the Always_populate_raw_post_data value in php.ini to ON, PHP will always fill in the post data into the variable $http_raw_post_data.

The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

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.