What is the difference between php://input and $_post?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
