如何在PHP中检查不完整的POST请求 [英] How to check for incomplete POST request in PHP

查看:149
本文介绍了如何在PHP中检查不完整的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当连接缓慢的远程Web客户端无法发送包含multipart/form-data内容的完整POST请求,但PHP仍使用部分接收到的数据填充$_POST数组时,我遇到了一个问题.结果,$_POST数组中的一个值可能不完整,并且可能缺少更多值.我试图在 Apache列表首先得到

I am facing a problem when a remote web client with slow connection fails to send complete POST request with multipart/form-data content but PHP still uses partially received data to populate $_POST array. As a result one value in $_POST array can be incomplete and more values can be missing. I tried to ask same question in Apache list first and got an answer that Apache doesn't buffer the request body and passes it to PHP module as a giant blob.

这是我的示例POST请求:

Here is my sample POST request:

POST /test.php HTTP/1.0
Connection: close
Content-Length: 10000
Content-Type: multipart/form-data; boundary=ABCDEF

--ABCDEF
Content-Disposition: form-data; name="a"

A
--ABCDEF

您可以看到Content-Length10000个字节,但是我只发送了一个var a=A.

You can see that Content-Length is 10000 bytes, but I send just one var a=A.

PHP脚本为:

<?php print_r($_REQUEST); ?>

Web服务器等待其余大约10秒的时间(但我什么也没发送),然后返回以下响应:

Web server waits for about 10 seconds for the rest of my request (but I don't send anything) and then returns this response:

HTTP/1.1 200 OK
Date: Wed, 27 Nov 2013 19:42:20 GMT
Server: Apache/2.2.22 (Debian)
X-Powered-By: PHP/5.4.4-14+deb7u3
Vary: Accept-Encoding
Content-Length: 23
Connection: close
Content-Type: text/html

Array
(
     [a] => A
)

这是我的问题:如何在PHP中验证是否完全收到了发帖请求? $_SERVER['CONTENT_LENGTH']将在请求标头中显示10000,但是有没有办法检查收到的实际内容长度?

So here is my question: How can I verify in PHP that the post request was received completely? $_SERVER['CONTENT_LENGTH'] would show 10000 from the request header, but is there a way to check the real content length received?

推荐答案

我猜想远程客户端实际上是带有HTML页面的浏览器. 否则,请告知我,我将尝试调整我的解决方案.

I guess that the remote client is actually a browser with HTML page. otherwise, let me know and i'll try to adapt my solution.

您可以添加字段<input type="hidden" name="complete">(例如)作为 last 参数.首先在PHP中检查此参数是否是从客户端发送的.如果发送了此参数,则可以确保您已获取全部数据.

You can add field <input type="hidden" name="complete"> (for example) as the last parameter. in PHP check firstly whether this parameter was sent from client. if this parameter sent - you can be sure that you got the entire data.

现在,我不确定是否必须根据RFC(包括HTML和HTTP)保留参数的顺序.但是我尝试了一些变化,但发现顺序确实保持了.

Now, i'm not sure that the order of parameters must be preserved according the RFC (of both, HTML and HTTP). but i've tried some variations and i saw that the order kept indeed.

更好的解决方案是(在客户端)计算参数的哈希并将其作为另一个参数发送.因此您可以绝对确定获得了所有数据.但这听起来很复杂...

Better solution will be, calculate (on client side) hash of the parameters and send him as another parameter. so you can be absolutely sure that you got the entire data. But this is starting to sound complicated...

这篇关于如何在PHP中检查不完整的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆