PHP Post and get

Source: Internet
Author: User
Tags cdata

As a computer system, input as a non-core device is indispensable, such as hardware, software is also the case. Imagine a powerful computer without input, unlike a piece of scrap iron that only consumes electricity and makes a buzzing noise. The same is true for applications.

In a Web application developed by PHP, all non-PHP code and output statements will be output, then how can the program receive input content? Believe that a lot of people must have thought of the Web Form! Under the HTTP protocol, the prototype of the input and output is actually an HTTP request and response, and the client sends the data to the server as a request, which is called a response. Although http/1.1 defines 7 kinds of request methods, the only ones that are commonly used are the two methods--get and post that are available in the http/0.9 species, both of which allow custom data to be sent to the server side. Therefore, you can implement the input by receiving the request data for both methods.

One, $_get array
Get is the most original request in HTTP, and a GET request is sent by clicking on a hyperlink in the Web page or entering a URL in the address bar. In a GET request, the data is the suffix sent after the URL, as if: [Url][/url]. PHP encapsulates the GET request in the $_get array, the requested variable name is the subscript of the array, to receive the two variable IDs and password of the request passed above, using $_get[' id ' and $_get[' password ']. Take a look at the following example:

<!–get.php file –><?phpif ($_get[' get ']) {echo $_get[' id '], "<BR>"; Echo $_get[' password '], "<BR>";}? ><form action= "get.php" ><label for= "id" > Account: </label><input type= "text" name= "id" >< Label for= "password" > Password: </label><input type= "text" name= "password" ><input type= "Submit" name= "get "Value=" ></form> "Submit"

The get.php file has a form that uses the default get method to send the request with the same effect as the URL you see above. Notice that the file begins with the PHP snippet: It first determines if the request variable get is present (whether or not it clicked the Submit button with the Name property as Get), and if so, the output requested variable. Because the variable get does not exist when the file is opened for the first time, the PHP code at the beginning is not executed at all, and the form is output directly. When the form is submitted, the PHP code is executed, and the two variables entered by the user are also output.

Sometimes, in a program is not sure what data the client submits, so how to traverse the request data without knowing the name of the request variable? This masking detail, the method of traversing the collection is called Iteration (iterate) in design mode, and PHP is implemented by using a foreach statement. Take a look at the following example:

<!–iterator.php–><?phpforeach ($_get as $index = $value) echo "$_get[$index] = $value", "<BR>";? >

Using Iterator.php?id=juxugongzi &password=adsl&address=peking This link to access it, you will see that the foreach statement enumerates all the variables that were requested by the Get method. And in advance, we don't know the name of each request variable.

Second, $_post array
The Get method is meant to be downloaded (instead of the upload method put), so it is not specifically used to pass data, it will request the data all through the URL encoding suffix behind the request resource, so that when the data is very large when the URL will become very long-but this is not the problem, The problem is that some Web browsers or server programs limit the length of this line of strings. At this point, you need to use the Post method.
As the name implies, the primary use of the Post method is to "pass" the data, which uploads the data behind all the request headers, so that no matter how much data is uploaded, the size of the request data depends on the size allowed by the Web service. In general, the Post method is used to upload form data for no particular need, so there is no need to care about the size of the specific upload data.
The request data for the Post method is encapsulated in the $_post array, using the same method as the $_get array. Adding a method attribute to the above form becomes:

<!–post.php file –><?phpif ($_post[' post ']) {foreach ($_post as $index = > $value) echo "$_post[$index] = $value", "<BR>";}? ><form action= "post.php" method= "POST" ><label for= "id" > Account: </label><input type= "text" Name= " ID "><label for=" password "> Password: </label><input type=" text "name=" password "><input type=" Submit "Name=" POST "value=" ></form>

Another interesting setting is that the Get method and the Post method are not contradictory, in the Post method can also pass the get variable, the above post.php file slightly changed, it becomes this:

<!–post.php file –><?phpif ($_post[' post ']) {foreach ($_post as $index = > $value) echo "$_post[$index] = $value", "<BR>"; foreach ($_get as $index = > $value) echo "$_get[$index] = $value", "<BR>";}? ><form action= "Post.php?act=login" method= "POST" ><label for= "id" > Account: </label><input type= " Text "name=" id "><label for=" password "> Password: </label><input type=" text "name=" password "><input Type= "Submit" name= "POST" value= "commit" ></form>

The program processes the request data of the Get Method (act parameter) and the Post method (ID and password parameter), the request variable suffix of the Get method is behind the value of the form Action property, so there is only one point, that is, the request variable of the Get method cannot be entered by the user. Typically, fixed data uses the Get method, which is sent by the data entered by the user using the Post method, and the two methods distinguish different logical data from each other. However, readers in the construction of this form, it is important to note that the two methods do not have the same variable name, or there will be unexpected consequences. Finally, it should be noted that when sending data using the Get method, the <form> Tag Action property value cannot contain a query string (even if it does not take effect), so the act parameter will not be sent in the following form:

<form action= "Post.php?act=login" method= "get" ><label for= "id" > Account: </label><input type= "Text" Name= "id" ><label for= "password" > Password: </label><input type= "text" name= "password" ><input type= "Submit" name= "POST" value= "Submission" ></form>

Should be changed to the following form:

<form action= "post.php" method= "get" ><label for= "id" > Account: </label><input type= "text" Name= "id" ><label for= "password" > Password: </label><input type= "text" name= "password" ><input type= "Submit" Name= "POST" value= "submit" ><input type= "hidden" name= "act" value= "login" ></form>

Three, $_request array
One problem with using $_get and $_post to receive the data passed is that the program must know which method to use for the data that is coming. But in fact, either way, the data is passed to the Web application, and they are intended to be the same. These details are masked by the $_request array, which encapsulates the contents of the four arrays of $_get, $_post, $_file, and $_cookie so that we can treat the data equally. In fact, in JSPs, the Get and post variables are uniformly received using the Request.getparameter () method, except that the $_request array in PHP encapsulates the contents of the cookie. When using the $_request array, the above example can be simplified to:

<!–request.php file –><?phpif ($_request[' post ']) {foreach ($_request as $index = > $value) echo "$_request[$ Index] = $value "," <BR> ";}? ><form action= "REQUEST.PHP?ID=JUXUGONGZI&PASSWORD=ADSL" method= "POST" ><label for= "id" > Account: </ Label><input type= "text" name= "id" ><label for= "password" > Password: </label><input type= "text" name = "Password" ><input type= "Submit" name= "POST" value= "Submit" ></form>

However, using the $_request array is important to note that the variable names in several data do not conflict, especially to note that the $_cookie, its data is not used by the client to set the

A few ways to get post data from PHP

Method 1, the most common method is: $_post[' fieldname '];

Note: Only data submitted by content-type:application/x-www-form-urlencoded can be received
Explanation: That is, the data that forms post

Method 2, File_get_contents ("Php://input");

Allows the raw data to be read from the POST.
Compared to $HTTP _raw_post_data, it brings less pressure to memory and does not require any special php.ini settings.
Php://input cannot be used for enctype= "Multipart/form-data".
For post data that does not specify Content-type, you can use File_get_contents ("Php://input") to get the raw data.
In fact, this method can be used for any data that is received by the post in PHP. Regardless of the Content-type, including binary file streams is also possible.
So using method Two is the safest method.

Method 3, $GLOBALS [' Http_raw_post_data '];

always produces $HTTP the _raw_post_data  variable contains the original POST data.
This variable is only generated when you encounter data with an unrecognized MIME type.
$HTTP _raw_post_data  is not available for enctype= "Multipart/form-data"   form data
If the POST data is not recognized by PHP, you can use $GLOBALS [' Http_raw_post_data '] to receive,
such as text/xml or soap, and so on
$GLOBALS [' Http_raw_post_data '] holds the raw data from POST.
$_post or $_request holds PHP in the form of a key=>value format for subsequent data.
But whether or not to save post data in $globals[' Http_raw_post_data '] depends on the settings of the Centent-type, that is, the post data must be explicitly indicated content-type:application/ The x-www-form-urlencoded,post data is stored in the $GLOBALS [' Http_raw_post_data ']

<xml>   <tousername><![ Cdata[touser]]></tousername>   <fromusername><![ cdata[fromuser]]></fromusername>   <CreateTime>1348831860</CreateTime>   <msgtype ><! [cdata[text]]></msgtype>   <content><![ Cdata[this is a test]]></content>   <MsgId>1234567890123456</MsgId>   <agentid>1 </AgentID></xml>

Method 2 (file_get_contents ("Php://input")):

$input = file_get_contents ("Php://input"); Receive post Data $xml = simplexml_load_string ($input); Extract post data as SimpleXML Object Var_dump ($xml);

Method 3 ($GLOBALS [' Http_raw_post_data '])

$input = $GLOBALS [' Http_raw_post_data '];libxml_disable_entity_loader (true); $xml = simplexml_load_string ($input, ' SimpleXMLElement ', libxml_nocdata); Var_dump ($xml);

PHP Post and get

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: 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.