Introduction to POST data receiving methods in PHP

Source: Internet
Author: User
Tags form post
This article briefly introduces three methods for receiving post data by php, and attaches a simple example, if you have a small partner, you can refer to the following situations to submit data to the server using a browser webpage form. We use PHP to receive data from the user 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

 

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 ));

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

For more information about how PHP receives POST data, refer to the PHP Chinese website!

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.