即使用户关闭浏览器,PHP 脚本还能继续运行吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1658443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:25:28  来源:igfitidea点击:

Can PHP scripts continue to run even if the user closed the browser?

php

提问by Oguz Bilgic

For example, there is a very simple PHP script which updates some tables on database, but this process takes a long time (maybe 10 minutes). Therefore, I want this script to continue processing even if the user closed the browser, because sometimes users do not wait and they close the browser or go to another webpage.

例如,有一个非常简单的 PHP 脚本更新数据库上的一些表,但是这个过程需要很长时间(可能 10 分钟)。因此,我希望即使用户关闭浏览器,这个脚本也能继续处理,因为有时用户不等待,他们关闭浏览器或转到另一个网页。

采纳答案by timdev

To answer your question directly, see ignore_user_abort

要直接回答您的问题,请参阅ignore_user_abort

More broadly, you probably have an architecture problem here.

更广泛地说,您可能在这里遇到了架构问题。

If many users can initiate this stuff, you'll want the web application to add jobs to some kind of queue, and have a set number of background processes that chew through all the work.

如果许多用户可以启动这些东西,您会希望 Web 应用程序将作业添加到某种队列中,并有一定数量的后台进程来处理所有工作。

回答by DavidWinterbottom

If the task takes 10 minutes, do not use a browser to execute it directly. You have lots of other options:

如果任务需要 10 分钟,请不要使用浏览器直接执行。您还有很多其他选择:

  • Use a cronjob to execute the task periodically.
  • Have the browser request insert a new row into a database table so that a regular cronjob can process the new row and execute the PHP script with the appropriate arguments
  • Have the browser request write a message to queue system, which has a subscriber listening for such events (which then executes the script).
  • 使用 cronjob 定期执行任务。
  • 让浏览器请求在数据库表中插入新行,以便常规 cronjob 可以处理新行并使用适当的参数执行 PHP 脚本
  • 让浏览器请求向队列系统写入一条消息,队列系统有一个订阅者监听此类事件(然后执行脚本)。

While some of these suggestions are probably overkill for your situation, the key, combining feature is to de-couple the browser request from the execution of the job, so that it can be completed asynchronously.

虽然其中一些建议对于您的情况可能有点过分,但关键的组合功能是将浏览器请求与作业的执行分离,以便它可以异步完成。

If you need the browser window updated with progress, you will need to use a periodically-executed AJAX request to retrieve the job status.

如果您需要随进度更新浏览器窗口,则需要使用定期执行的 AJAX 请求来检索作业状态。

回答by bcoughlan

The PHP script will keep running after the client terminates the connection (not doing so would be a security risk), but only up to max_execution_time(set in php.ini or through a PHP script, generally 30 seconds by default)..

PHP 脚本将在客户端终止连接后继续运行(不这样做会带来安全风险),但最多只能运行max_execution_time(在 php.ini 中或通过 PHP 脚本设置,默认情况下通常为 30 秒)。

For example:

例如:

<?php
    $fh = fopen("bluh.txt", 'w');
    for($i=0; $i<20; $i++) {
        echo $i."<br/>";
        fwrite($fh,$i."\n");
        sleep(1);
    }
    fclose($fh);
?>

Start running that in your browser and close the browser before it completes. You'll find that after 20 seconds the file contains all of the values of $i.

开始在浏览器中运行它并在它完成之前关闭浏览器。您会发现 20 秒后该文件包含$i.

Change the upper bound of the for loop to 100 instead of 20, and you'll find it only runs from 0 to 29. Because of PHP's max_execution_timethe script times out and dies.

将 for 循环的上限更改为 100 而不是 20,您会发现它只从 0 到 29 运行。由于 PHP 的原因max_execution_time,脚本超时并终止。

回答by yopefonic

if the script is completely server based (no feedback to the user) this will be done even if the client is closed.

如果脚本完全基于服务器(不向用户反馈),即使客户端关闭,也会执行此操作。

The general architecture of PHP is that a clients send a request to a script that gives a reply to the user. if nothing is given back to the user the script will still execute even if the user is not on the other side anymore. More simpler: their is no constant connection between server and client on a regular script.

PHP 的一般体系结构是客户端向脚本发送请求,该脚本对用户进行回复。如果没有给用户任何东西,即使用户不再在另一边,脚本仍然会执行。更简单:它们在常规脚本上没有服务器和客户端之间的持续连接。

回答by Annerajb

You can make the PHP script run every 20 minutes using a crontab file which contains the time and what command to run in this case it would be the php script.

您可以使用 crontab 文件使 PHP 脚本每 20 分钟运行一次,该文件包含时间以及在这种情况下要运行的命令是 php 脚本。

回答by anonymous

Yes. The server doesn't know if the user closed the browser. At least it doesn't notice that immediately.

是的。服务器不知道用户是否关闭了浏览器。至少它不会立即注意到这一点。

No: the server probably (depending of how it is configured) won't allow for a php script to run for 10 minutes. On a cheap shared hosting I wouldn't rely on a script running for longer than a reasonable response time.

否:服务器可能(取决于它的配置方式)不允许 php 脚本运行 10 分钟。在廉价的共享主机上,我不会依赖运行时间超过合理响应时间的脚本。

回答by shanyu

A server-side script will go on what it is doing regardless of what the client is doing.

无论客户端在做什么,服务器端脚本都会继续它正在做什么。

EDIT: By the way, are you sure that you want to have pages that take 10 minutes to open? I suggest you to employ a task queue (whose items are executed by cron on a timely basis) and redirect user to a "ok, I am on it" page.

编辑:顺便说一下,您确定要打开需要 10 分钟的页面吗?我建议您使用任务队列(其项目由 cron 及时执行)并将用户重定向到“好的,我在”页面。