简体   繁体   English

PHP可以检测到不完整的xmlhttprequest.send(data)命令吗?

[英]Can PHP detect an incomplete xmlhttprequest.send(data) command?

I'm seeing error logs caused by what appears to be truncation of the post data. 我看到错误日志是由于发布数据似乎被截断引起的。 After debugging, my guess is that the issue is that we're using an xmlhttprequest to do the post, and the user is closing the browser before the entirety of the post data has been sent. 调试之后,我的猜测是问题在于我们正在使用xmlhttprequest进行发布,并且用户在发送整个发布数据之前关闭了浏览器。

Here's our js code: 这是我们的js代码:

httpReq = new XMLHttpRequest();
httpReq.open("POST", url,false);
httpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpReq.send(param);

Is there a way to detect an incomplete send on the PHP side other than finding out there are missing post parameters? 除了找出缺少的post参数之外,是否有其他方法可以检测PHP方面的不完整发送? (Which we can't do since they're dynamic.) (因为它们是动态的,所以我们不能这样做。)

2011-02-27 Edit: 2011-02-27编辑:

As an example, the client is sending something like: 例如,客户端发送如下消息:

&obj1_name=Greeting&obj1_type=string&obj1_value=hello&obj1_id=34

The obj1_id is a required field in order to store the remaining details about the object. obj1_id是必填字段,用于存储有关对象的其余详细信息。 But sometimes we receive a string like: 但有时我们会收到类似以下的字符串:

&obj1_name=Greeting&obj1_type=string

My goal is to tell the difference between a broken client (one where it meant to send the short string) that we need to fix, and a working client that was closed too early (one where it meant to send the long string) and we can write off the error as unavoidable. 我的目标是告诉一个破碎的客户端(即一个意思发送短字符串)之间,我们需要解决和工作的客户端已关闭太早(即一个意思发送长字符串),我们的区别,可以避免不可避免的错误。

You can have a look at this function: connection_aborted . 您可以看一下这个函数: connection_aborted This approach will only work if you set ignore_user_abort(true) . 仅当您设置ignore_user_abort(true)此方法才有效。 The script will now continue to run even if the user hit the abort button. 现在,即使用户单击了中止按钮,该脚本也将继续运行。 You then have to check the connection_aborted function and discard the data if necessary. 然后,您必须检查connection_aborted函数,并在必要时丢弃数据。

Another approach would be to save some kind of flag at the end of the script to mark the data as completely processed. 另一种方法是在脚本末尾保存某种标志,以将数据标记为已完全处理。 If that flag is not set, you can discard the data. 如果未设置该标志,则可以丢弃数据。

I've realized we simply need more info from the client in order to do this. 我已经意识到我们只是需要来自客户端的更多信息才能做到这一点。 To accomplish this, we've added a known termination value on the client end. 为此,我们在客户端添加了一个已知的终止值。 If that final parameter isn't set when the server sees it, it was an incomplete submission. 如果服务器看到该参数时未设置该最终参数,则表示该提交不完整。

我不会这么想,因为PHP是服务器端的,而XmlHttpRequests是客户端的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM