GET, POST and REQUEST usage in PHP

Keywords: PHP Ajax

GET, POST and REQUEST usage in PHP

I$_ GET array

GET is the most primitive request method in HTTP. Clicking a hyperlink in a web page or entering a URL in the address bar will send a GET request. In the GET request, the data is sent after the URL, just like this: HTTP://www.phpboke.com/request.php?id=root&password=asdfl . PHP encapsulates GET requests in G E T number group in , please seek of change amount name yes number group of lower mark , want meet collect upper noodles that individual please seek pass Deliver of two individual change amount i d and p a s s w o r d , send use _ In the GET array, the requested variable name is the subscript of the array. To receive the two variable IDs and password passed by the above request, use In the G ¢ ET array, the requested variable name is the subscript of the array. To receive the two variable IDs and password passed by the above request, use_ GET ['id'] and$_ GET ['password']. Take the following example:

<?php
if($_GET['get']) {
echo $_GET['id'], "<BR>";
echo $_GET['password'], "<BR>";
}
?>
<form action="get.php">
<label for="id">account number:</label><input type="text" name="id">
<label for="password">password:</label><input type="text" name="password">
<input type="submit" name="get" value="Submit">
</form>

There is a form in the get.php file, which will send the request using the default get method, and its effect is the same as the URL seen above. Please pay attention to the PHP code segment at the beginning of the file: it first determines whether the request variable get exists (whether the submit button with the name attribute of get is clicked). If so, it outputs the requested variable. Since the variable get does not exist when the file is opened for the first time, the PHP code at the beginning will not be executed at all, and the form will be output directly. When the form is submitted, the PHP code is executed and the two variables entered by the user are output.

Sometimes, it is not certain what data the client has submitted in the program, so how to traverse the request data without knowing the name of the request variable? This method of masking details and traversing collections is called iteration in design patterns, which is implemented through foreach statements in PHP. Take the following example:

<?php
foreach($_GET as $index => $value) echo "$_GET[$index] = $value", "<BR>";
?>

Use the link iterator. PHP? Id = juxugungzi & password = ADSL & address = Peking to access it. You will see that the foreach statement enumerates all the variables requested by the GET method. In advance, we do not know the name of each request variable.

II$_ POST array

The GET method is intended to download (the corresponding upload method is PUT), so it is not specifically used to transfer data. It encodes all the requested data through the URL suffix after the requested resource. In this way, when there is a lot of data, 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 string. At this point, you need to use the POST method.
As the name suggests, the main purpose of the POST method is to "transfer" data. It uploads the data after all request headers. In this way, no matter how much data is uploaded, it will not be a problem (in this way, the size of the requested data depends on the size allowed by the WEB Service). Generally speaking, if there is no special need for form data, the POST method is used to upload it, so there is no need to care about the specific size of the uploaded data.
The request data of the POST method is encapsulated in P O S T number group in , his send use square method And _ In the POST array, it is used in the same way as In the P ^ OST array, its use method is the same as_ GET array. Add a method attribute to the above form, and it becomes as follows:

<?php
if($_POST['post']) {
foreach($_POST as $index = > $value) echo "$_POST[$index] = $value", "<BR>";
}
?>
<form action="post.php" method="post">
<label for="id">account number:</label><input type="text" name="id">
<label for="password">password:</label><input type="text" name="password">
<input type="submit" name="post" value="Submit">
</form>

Another interesting setting is that the GET method and the POST method are not contradictory. You can also pass the GET variable in the POST method. Change the above post.php file a little, and it will be like this:

<?php
if($_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 number:</label><input type="text" name="id">
<label for="password">password:</label><input type="text" name="password">
<input type="submit" name="post" value="Submit">
</form>

The program processes the request data of the GET method (act parameter) and the POST method (id and password parameter) at the same time. The request variable suffix of the GET method is after the action attribute value of the form. There is only one problem, that is, the request variable of the GET method cannot be entered by the user. Usually, the fixed data uses the GET method, and the data input by the user uses the POST method to send. The two methods distinguish the data with different logic. However, when building this form, readers must pay attention to that the two methods do not have the same variable name, otherwise there will be unpredictable consequences. Finally, it should be noted that when using the GET method to send data, the tag action attribute value cannot contain the query string (even if it is included, it will not take effect). Therefore, in the following form, the act parameter cannot be sent:

<form action="post.php?act=login" method="get">
<label for="id">account number:</label><input type="text" name="id">
<label for="password">password:</label><input type="text" name="password">
<input type="submit" name="post" value="Submit">
</form>

It should be changed to the following form:

<form action="post.php" method="get">
<label for="id">account number:</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>

III$_ REQUEST array

use G E T and _ GET and G. ET and_ There is a problem with POST receiving the transmitted data, that is, the program must know which method to use for the uploaded data. But in fact, no matter which method, it is to transfer data to WEB applications, and their purposes are the same. R E Q U E S T number group screen Cover Yes this some fine section , it seal pretend Yes _ The REQUEST array masks these details and encapsulates The R , request array masks these details and encapsulates_ GET, P O S T , _POST, P​OST,_ FILE and C O O K I E four individual number group of within Allow , send I Guys yes this some number according to can with one regard with kernel . matter real upper , stay J S P in , G E T and P O S T of change amount Just yes Unified one send use r e q u e s t . g e t P a r a m e t e r ( ) square method come meet collect , only no too P H P in of _ The contents of COOKIE's four arrays enable us to treat these data equally. In fact, in JSP, the variables of GET and POST are received using the request.getParameter() method, which is just a function in PHP The contents of the four arrays of C ^ OOKIE enable us to treat these data equally. In fact, in JSP, the variables of GET and POST are received using the request.getParameter() method, which is just a function in PHP_ The REQUEST array encapsulates the contents of the Cookie. When used$_ After the REQUEST array, the above example can be simplified as follows:

<?php
if($_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 number:</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, use R E Q U E S T number group yes one set want notes meaning , A few species number according to in of change amount name no want punching Outburst , especially his want notes meaning of yes _ For the REQUEST array, it is important to note that the variable names in several data do not conflict, especially For the R , request array, it must be noted that the variable names in several data do not conflict, especially_ COOKIE, whose data is not set by the client.

Posted by markhard on Tue, 05 Oct 2021 13:17:10 -0700