问题描述
我想运行一个相对耗时的脚本,基于某种形式的输入,但我不想诉诸于cron,所以我想知道一个php页面请求通过ajax将继续执行,直到完成或如果它将
它不会实际输出到浏览器,直到在文件的结尾处的json_encode,所以之前的一切仍然执行?
:
这似乎说你的问题的答案是是的,如果用户离开页面,脚本将终止。
但是意识到,根据使用的后端SAPI(例如 mod_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:
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执行是否停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!