Three ways to get post data from PHP

Source: Internet
Author: User
Tags http post

Method One, $_post

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

The $_post method is an array of variables passed through the HTTP POST method, which is an automatic global variable. If you use $_post[' name '] You can receive the Web form and the Web page asynchronously POST data, that is, $_post can only receive document type content-type:application/ X-www-form-urlencoded the submitted data.

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.

For example, the user uses a (client application) (JSON) post to the server for a file, the contents of which we do not care about, but we want to keep this file intact on the server, we can use the following code:

  

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 following is a small example that demonstrates $_post, $GLOBALS [' Http_raw_post_data '] and php://input receive POST data processing in three different ways:

A.html

<form name= "Demo_form" action= "post.php" method= "POST" >     <p><label>name: </label>< Input type= "text" class= "input" name= "name" ></p>     <p><label>address: </label>< Input type= "text" class= "input" name= "address" ></p>     

post.php

<?phpheader ("Content-type:text/html;charset=utf-8");  Echo ' $_post receives:<br/> '; Print_r ($_post); Echo ' 

  

Three ways to get post data from PHP

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.