如何在PHP响应Ajax请求后继续处理? [英] How to continue process after responding to ajax request in PHP?

查看:223
本文介绍了如何在PHP响应Ajax请求后继续处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些操作需要太多的时间,这导致Ajax请求超时。

Some operations take too much time, which lead the ajax request to time out.

我如何完成响应请求,再继续这样的操作?

How do I finish responding to the request first, then continue that operation?

推荐答案

ignore_user_abort 指令,并 ignore_user_abort 功能可能是你正在寻找的东西:它应该让你发送响应到浏览器,并在这之后,还是你的服务器上运行一些计算

The ignore_user_abort directive, and ignore_user_abort function are probably what you are looking for : it should allow you to send the response to the browser, and, after that, still run some calculations on your server.

这篇文章就您可能感兴趣的:如何使用ignore_user_abort()做加工带外;报价:
修改2010-03-22:删除链接的(指着的http:// waynepan.com/2007/10/11/怎么样! -to-的用ignore_user_abort - 待办事项进程出的带外/ ! - 删除空格和 如果你想试试)的,看到@Joel的意见后。

This article about it might interest you : How to Use ignore_user_abort() to Do Processing Out of Band ; quoting :
EDIT 2010-03-22 : removed the link (was pointing to http:// ! waynepan.com/2007/10/11/ ! how-to-use-ignore_user_abort-to-do-process-out-of-band/ -- remove the spaces and ! if you want to try ), after seeing the comment of @Joel.

基本上,当您使用    ignore_user_abort(真实)在你的PHP   脚本,该脚本将继续   运行即使用户pressed的   ESC或停止对他的浏览器。你怎么   用这个?
一个用途是   返回内容给用户,并允许   连接被关闭时   处理的事情,不需要   用户交互。

Basically, when you use ignore_user_abort(true) in your php script, the script will continue running even if the user pressed the esc or stop on his browser. How do you use this?
One use would be to return content to the user and allow the connection to be closed while processing things that don’t require user interaction.

下面的例子发出    $回应用户,在关闭   连接(使浏览器的   微调/加载条停止),然后   执行时    do_function_that_takes_five_mins();

The following example sends out $response to the user, closing the connection (making the browser’s spinner/loading bar stop), and then executes do_function_that_takes_five_mins();

和给定的例子:

ignore_user_abort(true);
header("Connection: close");
header("Content-Length: " . mb_strlen($response));
echo $response;
flush();
do_function_that_takes_five_mins();

(还有更多我没有复制粘贴)


请注意,你的PHP脚本仍然以适应的max_execution_time memory_limit的的限制 - 这意味着你不应该使用这对于需要时的操作。


Note that your PHP script still has to fit in the max_execution_time and memory_limit constraints -- which means you shouldn't use this for manipulations that take too much time.

这也将使用一个Apache进程 - 这意味着你不应该有几十页的做到这一点的同时

This will also use one Apache process -- which means you should not have dozens of pages that do that at the same time.

不过,漂亮的把戏,提升使用体验,我想, - )

Still, nice trick to enhance use experience, I suppose ;-)

这篇关于如何在PHP响应Ajax请求后继续处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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