PHP $_POST variable

In the above section we talked about the $_GET variable, so in this section we will learn about the $_POST variable to collect the value in the form with method="post".

$_POST variable

The predefined $_POST variable is used to collect values ​​from the form with method="post".

Information sent from a form with the POST method is invisible to anyone (will not be displayed in the browser's address bar), and there is no limit on the amount of information sent.

Note: However, by default, the maximum amount of information sent by the POST method is 8 MB (can be changed by setting post_max_size in the php.ini file).

Now let’s look at it with an example


     <meta charset="utf-8">
 <form action="php_post.php" method="post">
     名字: <input type="text" name="name"><br>
     性别:<input type="radio" name="sex" value="男" checked>男
     <input type="radio" name="sex" value="女">女<br>
     年龄: <input type="text" name="age"><br>
     <input type="submit" value="提交">

Submit to php_post.php page

 header("Content-type:text/html;charset=utf-8");    //设置编码
 echo "你的姓名是:" .$_POST['name'] ."<br/>";
 echo "你的性别是:"  .$_POST['sex']."<br/>";
 echo "你的年龄是:".$_POST['age'];

Program running results:


We see, The information we submitted using POST is not displayed in the Url column, so you now know that POST is more secure than GET

The difference between GET and POST:

  1. get is to obtain data from the server, and post is to transmit data to the server.

  2. get adds the parameter data queue to the URL pointed to by the ACTION attribute of the submitted form. The value corresponds one-to-one to each field in the form, which can be seen in the URL. . Post uses the HTTP post mechanism to place each field in the form and its content in the HTML HEADER and transmit it to the URL address pointed to by the ACTION attribute. Users cannot see this process.

  3. For the get method, the server side uses Request.QueryString to obtain the value of the variable. For the post method, the server side uses Request.Form to obtain the submitted data.

  4. The amount of data transferred by get is small and cannot be larger than 2KB. The amount of data transmitted by post is relatively large and is generally unrestricted by default. But in theory, the maximum amount is 80KB in IIS4 and 100KB in IIS5.

  5. get security is very low, post security is high. But the execution efficiency is better than the Post method.

1. The get method is less secure than the post method, including For confidential information, it is recommended to use the Post data submission method;

2. When doing data query, it is recommended to use the Get method; when doing data addition, modification or deletion, it is recommended to use the Post method;

PHP $_REQUEST variable

##The predefined $_REQUEST variable includes $_GET, $_POST and the contents of $_COOKIE.

$_REQUEST variable can be used to collect form data sent via GET and POST methods.

Modify the above php file and take a look

 header("Content-type:text/html;charset=utf-8");    //设置编码
 echo "你的姓名是:" .$_REQUEST['name'] ."<br/>";
 echo "你的性别是:"  .$_REQUEST['sex']."<br/>";
 echo "你的年龄是:".$_REQUEST['age'];

You will find that whether it is submitted by $_POST or $_GET, it can be received with $_REQUEST

Next Section

<html> <head> <meta charset="utf-8"> <title>php中文网(</title> </head> <body> <form action="php_post.php" method="post"> 名字: <input type="text" name="name"><br> 性别:<input type="radio" name="sex" value="男" checked>男 <input type="radio" name="sex" value="女">女<br> 年龄: <input type="text" name="age"><br> <input type="submit" value="提交"> </form> </body> </html>
submitReset Code