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

查看:19
本文介绍了用户离开页面后 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_encode 之前,它实际上不会输出到浏览器,那么之前的所有内容还会执行吗?

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?

推荐答案

视情况而定.

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

当 PHP 脚本正常运行时正常状态,是活动的.如果远程客户端断开 ABORTED状态标志打开.一个遥控器客户端断开连接通常是由用户点击了他的停止按钮.

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 Apachehttpd.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(),但具有输出缓冲 on 将导致这些调用陷入陷阱,并且在脚本完成之前不会将它们发送到客户端!

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天全站免登陆