Introduction to receiving POST data in PHP, Introduction to receiving post in php

Source: Internet
Author: User
Tags form post

Introduction to receiving POST data in PHP, Introduction to receiving post in php

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.

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

 <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>   <p><input type="submit" name="submit" class="btn" value="Submit"></p> </form> 

Post. php

Header ("Content-type: text/html; charset = UTF-8"); echo '$ _ POST receiving: <br/>'; print_r ($ _ POST ); echo '

The above is all the content in this article. I hope you can understand the three methods for receiving post data in php.

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.