难道一个用户离开该页面后,PHP执行停止? [英] Does php execution stop after a user leaves the page?

查看:150
本文介绍了难道一个用户离开该页面后,PHP执行停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个相对时间基于某种形式的输入消费剧本,但我宁愿不诉诸于cron的,所以我想知道如果通过AJAX请求一个PHP页面会继续下去,直到完成执行,或者如果它会如果用户离开页面停止

I want to run a relatively time consuming script based on some form input, but I'd rather not resort to cron, so I'm wondering if a php page requested through ajax will continue to execute until completion or if it will halt if the user leaves the page.

实际上它并不输出到浏览器,直到一个json_en code在文件的结尾,所以将一切之前仍然执行?

It doesn't actually output to the browser until a json_encode at the end of the file, so would everything before that still execute?

推荐答案

这要看情况。

从<一个href="http://us3.php.net/manual/en/features.connection-handling.php">http://us3.php.net/manual/en/features.connection-handling.php:

在PHP脚本运行正常   正常状态下,是有效的。如果   远程客户端断开ABORTED   状态标记将被打开。远程   客户端断开连接多因   用户击中他的STOP按钮。

When a PHP script is running normally the NORMAL state, is active. If the remote client disconnects the ABORTED state flag is turned on. A remote client disconnect is usually caused by the user hitting his STOP button.

您可以决定是否要   客户端断开造成的   脚本被中止。有时它是   方便总是有你的脚本运行   到完成,即使是没有   远程浏览器接收输出。   默认行为是但是,对于   你的脚本被中止时,   远程客户端断开连接。本   行为可以通过设置    ignore_user_abort php.ini指令为   以及通过相应    php_value ignore_user_abort 阿帕奇   的httpd.conf指令或与    ignore_user_abort()的功能。

You can decide whether or not you want a client disconnect to cause your script to be aborted. Sometimes it is handy to always have your scripts run to completion even if there is no remote browser receiving the output. The default behaviour is however for your script to be aborted when the remote client disconnects. This behaviour can be set via the ignore_user_abort php.ini directive as well as through the corresponding php_value ignore_user_abort Apache httpd.conf directive or with the ignore_user_abort() function.

这似乎是说,回答你的问题是是的,如果用户离开网页的脚本将终止。

That would seem to say the answer to your question is "Yes, the script will terminate if the user leaves the page".

不过意识到这取决于后端SAPI正在使用(例如, mod_php的)的php无法检测到客户端已经中止的连接直到试图将信息发送到客户端的。如果长时间运行的脚本不会发出的flush()脚本可能会继续即使用户已经关闭了连接运行。

However realize that depending on the backend SAPI being used (eg, mod_php), php cannot detect that the client has aborted the connection until an attempt is made to send information to the client. If your long running script does not issue a flush() the script may keep on running even though the user has closed the connection.

复杂化的事情是,即使你的问题定期调用的flush(),其的output的缓冲将导致这些调用陷阱,直到脚本完成反正不会派他们到客户端!

Complicating things is even if you do issue periodic calls to flush(), having output buffering on will cause those calls to trap and won't send them down to the client until the script completes anyway!

更多的复杂的事情是,如果你已经安装了Apache处理程序缓冲反应(例如 mod_gzip的),然后再次PHP将无法检测到连接被关闭,该脚本将继续卡车。

Further complicating things is if you have installed Apache handlers that buffer the response (for example mod_gzip) then once again php will not detect that the connection is closed and the script will keep on trucking.

唷。

这篇关于难道一个用户离开该页面后,PHP执行停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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