用户离开页面后,php执行是否停止? [英] Does php execution stop after a user leaves the page?

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

问题描述

我想运行一个相对耗时的脚本,基于某种形式的输入,但我不想诉诸于cron,所以我想知道一个php页面请求通过ajax将继续执行,直到完成或如果它将



它不会实际输出到浏览器,直到在文件的结尾处的json_encode,所以之前的一切仍然执行?



http://us3.php.net/manual/en/features.connection-handling.php


当PHP脚本正常运行时,
处于NORMAL状态,如果
远程客户端断开了ABORTED
状态标志的打开。远程
客户端断开通常是由
用户击中他的STOP按钮引起的。



您可以决定是否要
a客户端断开连接,导致
脚本中止。有时它是
方便总是让你的脚本运行
完成,即使没有
远程浏览器接收输出。
然而,对于
,默认行为是当
远程客户端断开连接时中止脚本。这个
行为可以通过
ignore_user_abort php.ini指令设置为
以及通过相应的
php_value ignore_user_abort Apache
httpd.conf指令或
ignore_user_abort()函数。


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



但是意识到,根据使用的后端SAPI(例如 mod_php ), php无法检测到客户端已中止连接,直到尝试向客户端发送信息 。如果长时间运行的脚本不发出 flush(),脚本可能会继续运行,即使用户已关闭连接。



复杂的事情即使您 发出 flush()的定期调用,具有输出缓冲将导致这些调用陷阱,并且不会将它们发送到客户端,直到脚本完成无论如何!



进一步复杂的事情是,如果你安装了Apache处理程序缓冲响应(例如 mod_gzip ),然后再次php将不会检测到连接已关闭,脚本将继续卡车。



Phew。


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.

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?

解决方案

It depends.

From http://us3.php.net/manual/en/features.connection-handling.php:

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.

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".

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.

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!

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.

Phew.

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

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