Home >Backend Development >PHP Problem >What is the difference between php://input and $_post?

What is the difference between php://input and $_post?

青灯夜游
青灯夜游Original
2020-04-25 16:47:402579browse

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.

What is the difference between php://input and $_post?

A few sentences extracted from the manual:

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

  2. 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(&#39;php://input&#39;, &#39;r&#39;));
    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 &#39;name=saisai&submit=submit&#39; (length=25)

----------post---------

array (size=2)
  &#39;name&#39; => string &#39;saisai&#39; (length=6)
  &#39;submit&#39; => string &#39;submit&#39; (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 &#39;&#39; (length=0)

----------post---------

array (size=2)
  &#39;name&#39; => string &#39;saisai&#39; (length=6)
  &#39;submit&#39; => string &#39;submit&#39; (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 &#39;&#39; (length=0)

----------post---------

array (size=2)
  &#39;name&#39; => string &#39;saisai&#39; (length=6)
  &#39;submit&#39; => string &#39;submit&#39; (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 &#39;name=saisai

submit=submit

&#39; (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:

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

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

  3. $_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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn