Summary of PHP methods for receiving POST data-PHP source code

Source: Internet
Author: User
Tags form post
The common method to accept POST data is $ _ POST. In fact, there are many function changes that can be accepted in addition to this method. Let's take a look at the following for details. The common method to accept POST data is $ _ POST. In fact, there are many function changes that can be accepted in addition to this method. Let's take a look at the following for details.

Script ec (2); script


Generally, users use browser web forms to submit data to the server. We use PHP to receive the data that users post to the server and perform proper processing. However, in some cases, if the user uses the client software to send post data to the php program on the server, rather than using $ _ POST for identification, what should he do?


$ _ POST receive data

$ _ POST is an array of variables passed through the http post method, which is an automatic global variable. For example, you can use $ _ POST ['name'] to receive data from webpage forms and webpage asynchronous post methods. That is, $ _ POST can only receive Content-Type documents: data submitted by application/x-www-form-urlencoded.

$ GLOBALS ['HTTP _ RAW_POST_DATA '] method to receive data
If the post data is not a document type that PHP can recognize, such as text/xml or soap, we can use $ GLOBALS ['HTTP _ RAW_POST_DATA '] to receive it. The $ HTTP_RAW_POST_DATA variable contains the original POST data. This variable is generated only when data of the unrecognized MIME type is encountered. $ HTTP_RAW_POST_DATA is unavailable for enctype = "multipart/form-data" form data. That is to say, using $ HTTP_RAW_POST_DATA cannot receive data from the webpage form post.
Php: // receives data in input mode
A better way to access the original POST data is php: // input. Php: // input allows reading original POST data. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings, while php: // input cannot be used for enctype = "multipart/form-data ".
For example, if a user uses a client application to post a file to the server, the content of the file is retained, but we want to save the file completely on the server, we can use the following code:

$ Input = file_get_contents ('php: // input ');
File_put_contents ($ original, $ input); // $ original indicates the file on the server.

The above Code uses file_get_contents ('php: // input') to receive post data and then write the data into the $ original file. In fact, it can be understood that a file is uploaded from the client to the server, there are a lot of such applications, especially when PHP development is required to develop products jointly with C, C ++ and other applications. For example, there are articles on this site: photo upload uses flash to upload photos.
The following is a small example that demonstrates receiving POST data processing in three different ways: $ _ POST, $ GLOBALS ['HTTP _ RAW_POST_DATA '] and php: // input:

A.html


Post. php

Header ("Content-type: text/html; charset = UTF-8 ");

Echo '$ _ POST receiving:
';
Print_r ($ _ POST );
Echo'

';

Echo '$ GLOBALS [\ 'HTTP _ RAW_POST_DATA \'] receive:
';
Print_r ($ GLOBALS ['HTTP _ RAW_POST_DATA ']);
Echo' ';

Echo 'php: // input receiving:
';
$ Data = file_get_contents ('php: // input ');
Print_r (urldecode ($ data ));

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.