_php tips for using php://input in PHP to process form data with the same name value

Source: Internet
Author: User

Last October, I was in the blog analysis of PHP received the same foreground name value of the same form submission data processing problem, at that time said the scheme is to change the name value of the group type, PHP received, and then the array of combined processing. The disadvantage of this is that it is not fully compatible with the foreground form, and the user must make changes to the form, and the system requirement is that all upgrades are completely transparent to the user, so this approach is not perfect.

After almost a year of precipitation, found that PHP provides the original access to the input/output stream method, post data can be obtained through Php://input:

Copy Code code as follows:

Php://input is a read-only stream that can access the requested raw data. In the case of POST requests, it is best to use php://input instead of $HTTP _raw_post_data because it does not depend on specific php.ini directives. Also, in such cases $HTTP _raw_post_data is not populated by default, and less memory is potentially needed than activating always_populate_raw_post_data. Enctype= "Multipart/form-data" when the Php://input is invalid.
The Php://input Open data stream can only be read once, and the data stream does not support seek operations. However, depending on the SAPI implementation, when the request body data is saved, it can open another php://input data stream and re-read it. Typically, this situation is only for POST requests, not other requests, such as put or PROPFIND.

So the idea of using PHP to get the same name value form data is this, 1, to get the original post data through Php://input; 2, the data is processed and merged; 3, re-assign the processed value to the system variable $_post; The following is a function defined:

function Get_submit () {
 if (empty ($_post)) return $_post;
 To determine the commit type
 if ($_server["Http_content_type"]!= ' application/x-www-form-urlencoded ') {return
 $_post;
 }
 Gets the post original value
 $data = file_get_contents ("Php://input");
 if (empty ($data)) return $_post;
 Start processing
 $POST =array ();
 $list =explode (' & ', $data);
 foreach ($list as $key => $value) {
 //Get Post's key and value value
 $postname =urldecode (substr ($value, 0, Stripos ($ Value, "="));
 $postvalue =urldecode (substr ($value, (Stripos ($value, "=") +1));
 Handle the key value and value values
 //go to the spaces and []
 $postname =trim ($postname, ', [,] ');
 $postvalue =trim ($postvalue);
 if (Array_key_exists ($postname, $POST)) {
  $POST [$postname]= $POST [$postname]. ",". $postvalue;
 } else{
  $POST [$postname]= $postvalue;
 }
 }
 return $POST;
}

Next, as long as you need to use "$_post=get_submit ()", then you can get the data for each form after processing by $_post.

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.