search
Homephp教程php手册PHP中使用php://input处理相同name值的表单数据

这篇文章主要介绍了PHP中使用php://input处理相同name值的表单数据,本文是另一种处理相同name值表单数据的方法,文中同时给出另一种方法,需要的朋友可以参考下

在去年10月份,我在博客中分析了php接收前台name值相同的表单提交数据的处理的问题,当时说的方案是为把name值改成数组类型,php接收到之后,再对数组进行合并处理。这样做的缺点就是不完全兼容前台表单,用户必须要对表单作出更改,而系统需求是所有升级对用户完全透明,所以说这种方式还是不够完美。

经过差不多一年时间的沉淀,发现php提供原始的访问输入/输出流的方法,POST的数据可以通过php://input来获取:

复制代码 代码如下:


php://input 是个可以访问请求的原始数据的只读流。 POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA,因为它不依赖于特定的 php.ini 指令。 而且,这样的情况下 $HTTP_RAW_POST_DATA 默认没有填充, 比激活 always_populate_raw_post_data 潜在需要更少的内存。 enctype=”multipart/form-data” 的时候 php://input 是无效的。
php://input 打开的数据流只能读取一次; 数据流不支持 seek 操作。 不过,,依赖于 SAPI 的实现,请求体数据被保存的时候, 它可以打开另一个 php://input 数据流并重新读取。 通常情况下,这种情况只是针对 POST 请求,而不是其他请求方式,比如 PUT 或者 PROPFIND。

所以用php获取相同name值的表单数据的思路可以是这样,1,通过php://input获取原始的POST数据; 2,对数据进行处理合并; 3, 重新把处理过的值赋给系统变量$_POST; 下面是定义的一个函数:

function GET_SUBMIT(){ if(empty($_POST)) return $_POST; //判断提交类型 if($_SERVER["HTTP_CONTENT_TYPE"] != 'application/x-www-form-urlencoded'){ return $_POST; } //获取POST原始值 $data= file_get_contents("php://input"); if(empty($data)) return $_POST; //开始处理 $POST=array(); $list=explode('&',$data); foreach($list as $key=>$value){ //获取POST的KEY和Value值 $postname=urldecode(substr($value,0, stripos($value,"="))); $postvalue=urldecode(substr($value,(stripos($value,"=")+1))); //对KEY值和Value值进行处理 //去空格和[] $postname=trim($postname,' ,[,]'); $postvalue=trim($postvalue); if(array_key_exists($postname,$POST)){ $POST[$postname]=$POST[$postname]." ,".$postvalue; }else{ $POST[$postname]=$postvalue; } } return $POST; }

接下来只要在需要的地方使用”$_POST=GET_SUBMIT()”, 后面就可以通过$_POST来获取处理之后各个表单的数据。

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

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),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!