bash 如果用户在 AJAX 请求结束之前退出浏览器或更改页面会发生什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8916690/
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-09-18 01:21:01  来源:igfitidea点击:

What happens if a user exits the browser or changes page before an AJAX request is over

phpjqueryajaxbashshell

提问by NaturalBornCamper

I am calling a php script over ajax to do some database maintenance. If the user closes the page, hits back, or clicks a link, will the php script be fully executed? Is there a way to do it?

我正在通过 ajax 调用 php 脚本来进行一些数据库维护。如果用户关闭页面、回击或点击链接,php 脚本会完全执行吗?有没有办法做到这一点?

Maybe if the php script called the exec()method or something similar, which would in turn call a script via the console as such:

也许如果 php 脚本调用了该exec()方法或类似的东西,它又会通过控制台调用一个脚本,如下所示:

$ php /var/www/httpdocs/maintenance.php

?

?

采纳答案by cdeszaq

As long as the user agent (browser, etc.) has fully sent the request, the server has all it needs and will complete the request and try to send back a response.

只要用户代理(浏览器等)完全发送了请求,服务器就会拥有它所需要的一切,并将完成请求并尝试发回响应。

In fact, this sort of "pinging" behavior is often used for "heartbeat"-like processes that keep a service warm or perform periodic maintenance.

事实上,这种“ping”行为通常用于保持服务温暖或执行定期维护的类似“心跳”的进程。

回答by Marc B

It's a race condition. PHP will detect at some point (usually upon attempting to do output) that Apache is yelling in its face that the remote user has closed the connection. Whether everything you wanted to do is done at that point depends on how your code's structured.

这是一个竞争条件。PHP 会在某个时候(通常是在尝试输出时)检测到 Apache 正在大喊远程用户已关闭连接。您想要做的一切是否在那时完成取决于您的代码的结构。

If you want to ensure that all operations are complete before the script shuts itself down, use ignore_user_abort(TRUE), which keeps PHP running after the connection is severed. It's still subject to the user max_execution_time limits and whatnot, but it will not shut down because you disconnected.

如果您想确保在脚本自行关闭之前完成所有操作,请使用ignore_user_abort(TRUE),它可以在连接断开后保持 PHP 运行。它仍然受用户 max_execution_time 限制和诸如此类的限制,但它不会因为您断开连接而关闭。

回答by Tejs

Once the web request makes it to your server, it really doesn't matter if the user closes their browser or navigates away. Your server will still respond, but no one will be listening for the response.

一旦 Web 请求发送到您的服务器,用户是关闭浏览器还是离开都没有关系。您的服务器仍然会响应,但没有人会监听响应。

回答by Has QUIT--Anony-Mousse

Varies on the settings, web server, operating system and so on.

因设置、Web 服务器、操作系统等而异。

Usually the request will be processed as usual, and the response will just never be read. Occasionally, a write might fail earlier, and the request fails while processing.

通常请求会像往常一样被处理,并且响应永远不会被读取。有时,写入可能会更早失败,并且请求在处理时失败。

回答by OnResolve

Once the ajax call is kicked off, the user is free to do whatever they want. If they close the page they simply won't get the feedback (if any ) from the ajax call that was made.

一旦 ajax 调用开始,用户就可以自由地做任何他们想做的事情。如果他们关闭页面,他们将无法从进行的 ajax 调用中获得反馈(如果有)。

回答by nobody

If the php starts executing then it will continue to execute regardless if the user closes the window or navigates away from the page.

如果 php 开始执行,那么无论用户是关闭窗口还是离开页面,它都会继续执行。

回答by Reinstate Monica Cellio

The php script will complete, regardless of browser state. The php is parsed on the server, and that doesn't care about whether the client is still open or not.

无论浏览器状态如何,php 脚本都会完成。php是在服务器端解析的,不管客户端是否打开。

回答by Alex Turpin

If the HTTP request was completed, then yes, the PHP script will be executed fully even if the client's computer is closed.

如果 HTTP 请求已完成,则是的,即使客户端的计算机关闭,PHP 脚本也会完全执行。