on how to receive post data in PHP

Source: Internet
Author: User
Tags form post http post

This article gives you a brief introduction of the next PHP to receive post data 3 ways, and attached a simple example, the need for small partners can refer to the

Typically, users use a browser Web page form to submit data to the server post, and we use PHP to receive data from the user post to the server and to handle it appropriately. But in some cases, if the user uses the client software to send post data to the server-side PHP program, instead of using $_post to identify, then how to deal with it?

Receive data in $_post mode

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

Receive Data $GLOBALS [' Http_raw_post_data ']

If the data that was posted is not the type of document that PHP can recognize, such as text/xml or soap, we can receive it with $globals[' Http_raw_post_data '. The $HTTP _raw_post_data variable contains the original POST data. This variable is generated only when data with an unrecognized MIME type is encountered. $HTTP _raw_post_data is not available for enctype= "multipart/form-data" form data. That is, you cannot receive data from a Web Form POST by using $http_raw_post_data.

Receive data in Php://input mode

A better way to access the original POST data is php://input. Php://input allows you to read the original data for the POST. Compared with $HTTP _raw_post_data, it brings less pressure on memory and does not require any special php.ini settings, and php://input cannot be used enctype= "Multipart/form-data".

For example, a user uses a client application to post to the server a file, the contents of the file we ignore it, but we want to keep this file on the server completely, we can use the following code:

?

1 2 3 $input = file_get_contents (' php://input '); File_put_contents ($original, $input); $original to files on the server

The above code uses file_get_contents (' Php://input ') to receive post data, and then writes the data to the $original file, which can actually be understood as uploading a file from the client to the server, such applications are very many, In particular, our PHP development will be used with c,c++ and other application development for product joint development.

The following is a small example that demonstrates $_post, $GLOBALS [' Http_raw_post_data '] and php://input three different ways of receiving POST data processing:

A.html

?

1 2 3 4 5 6 <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

?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 Header ("Content-type:text/html;charset=utf-8"); Echo ' $_post receive:<br/> '; Print_r ($_post);   Echo '

The above is the entire content of this article, I hope you can receive the post data in PHP 3 ways to know something.

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.