search
Homephp教程PHP开发Detailed examples of three methods for obtaining POST data in PHP

Three ways for PHP to obtain POST data

Method one, $_POST

$_POST or $_REQUEST stores the data formatted by PHP in the form of key=>value.

Method 2, use file_get_contents("php://input")

For POST data without specified Content-Type, you can use file_get_contents("php://input"); to obtain the original data. In fact, any data received by POST using PHP uses this method. Regardless of Content-Type, including binary file streams is also feasible.

Compared with $HTTP_RAW_POST_DATA, it puts less pressure on memory and does not require any special php.ini settings.

php://input cannot read POST data whose Content-Type is multipart/form-data. You need to set the always_populate_raw_post_data value in php.ini to On.

php://input cannot read $_GET data. This is because the $_GET data is written in the PATH field of the http request header as query_path, rather than in the body part of the http request.

Method three, use the global variable $GLOBALS['HTTP_RAW_POST_DATA']

The original data from POST is stored in $GLOBALS['HTTP_RAW_POST_DATA'].

But whether the POST data is saved in $GLOBALS['HTTP_RAW_POST_DATA'] depends on the centent-Type setting. Only when PHP cannot recognize the Content-Type, the POST data will be filled in as it is. Enter the variable $GLOBALS['HTTP_RAW_POST_DATA']. When Content-Type=application/x-www-form-urlencoded, the variable is empty.

In addition, it is also unable to read POST data whose Content-Type is multipart/form-data. You also need to set the always_populate_raw_post_data value in php.ini to On, so that PHP will always fill in the POST data into the variable $http_raw_post_data.

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed examples of the three methods of obtaining POST data in PHP, please pay attention to the PHP Chinese website for related articles!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment