What is the difference between php://input and $_post? The following article will introduce it to you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
A few sentences extracted from the manual:
When HTTP POST request When Content-Type is application/x-www-form-urlencoded or multipart/form-data, the variables will be passed into the current script in the form of an associative array.
php://input is a read-only stream that provides access to the requested raw data. When enctype="multipart/form-data" is used, php://input is invalid.
Verify:
post.html
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="getpost.php" method="post"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form> </body> </html>
getpost.php
<?php echo "----------input--------<br />"; var_dump(file_get_contents('php://input', 'r')); echo "----------post---------<br />"; var_dump($_POST); ?>
1. enctype ="application/x-www-form-urlencoded"
Request body:
Content-Type: application/x-www-form-urlencoded Content-Length: 25name=saisai&submit=submit
Output:
----------input-------- string 'name=saisai&submit=submit' (length=25) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
Summary: When enctype="application/x- www-form-urlencoded", the data (name=saisai&submit=submit) in the request body is converted into an associative array and put into $_POST, while php://input obtains the original data (raw data).
2. When enctype="multipart/form-data"
2.1 Form:
<form action="getpost.php" method="post" enctype="multipart/form-data"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form>
Request subject:
Content-Type: multipart/form-data; boundary=---------------------------22554656810024 Content-Length: 249 -----------------------------22554656810024 Content-Disposition: form-data; name="name" saisai -----------------------------22554656810024 Content-Disposition: form-data; name="submit" submit -----------------------------22554656810024--
Output:
----------input-------- string '' (length=0) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
Summary: When enctype="multipart/form-data" and there is no upload file control, $_POST can print data normally, but php:// is invalid.
2.2 Form (add a file upload):
<form action="getpost.php" method="post" enctype="multipart/form-data"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form>
Request subject:
Content-Type: multipart/form-data; boundary=---------------------------272321281228527 Content-Length: 68386 -----------------------------272321281228527 Content-Disposition: form-data; name="name" saisai -----------------------------272321281228527 Content-Disposition: form-data; name="filename"; filename="dog.png" Content-Type: image/png 一堆乱码 -----------------------------272321281228527 Content-Disposition: form-data; name="submit" submit -----------------------------272321281228527--
Output:
----------input-------- string '' (length=0) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
Summary: In enctype="multipart/form -data" and there is an upload file control, $_POST can print out the incoming data, but excludes any uploaded content. php:// is not valid.
3. enctype="text/plain"
Form:
<form action="getpost.php" method="post" enctype="text/plain"> <input type="text" name="name" value="saisai"> <input type="submit" name="submit" value="submit"> </form>
Request body:
Content-Type: text/plain Content-Length: 28 name=saisai submit=submit
Output:
----------input-------- string 'name=saisai submit=submit ' (length=28) ----------post--------- array (size=0) empty
Summary: When enctype="text/plain", there is no content in $_POST, and it is stored in key-value pairs in php://input.
Summary:
When the Content-Type of the HTTP POST request is application/x-www-form-urlencoded or multipart/form-data : php://input contains the original data similar to a=1&b=2. $_POST contains an associative array and does not upload the content of the control.
php://input is a read-only stream that provides access to the requested raw data. When enctype="multipart/form-data" is used, php://input is invalid.
$_POST cannot obtain post data when Content-Type = "text/plain", but php://input can.
For more related knowledge, please pay attention to PHP Chinese website! !
The above is the detailed content of What is the difference between php://input and $_post?. For more information, please follow other related articles on the PHP Chinese website!