简体   繁体   English

PHP后台脚本阻止整个浏览器的连接

[英]PHP background Script blocking connection for whole browser

I have the following problem: 我有以下问题:

A PHP Script is called via Ajax. 通过Ajax调用PHP脚本。 The script itself closes the connection using the following lines 脚本本身使用以下几行关闭连接

ignore_user_abort(true);
header("Content-Length: 0");
header("Connection: close");
flush();

and then starts sending out a bunch of mails in the background using phpMailer (in a loop and using sleep(1) after each mail - in the future I want this to be a random amount of seconds after 5 sent mails). 然后开始使用phpMailer在后台发送一堆邮件(循环并在每封邮件后使用sleep(1)-将来,我希望这是5封邮件后的随机秒数)。 Using FireBug, I can see that the connection to the script is directly terminated. 使用FireBug,我可以看到与脚本的连接已直接终止。 The mails are being sent as well. 邮件也被发送。

However, I cannot open any pages while the script is running in the background - they keep loading until the background script obviously has finished. 但是,当脚本在后台运行时,我无法打开任何页面-它们会一直加载,直到后台脚本明显完成为止。 The strange thing: The script doesn't seem to block the whole server, as the connection restriction only applies to the browser that initiated the background script. 奇怪的是:该脚本似乎并没有阻塞整个服务器,因为连接限制仅适用于启动后台脚本的浏览器。 So after I start the script in - let's say - FireFox, I can still access the pages on the server in Chrome. 因此,当我在FireFox中启动脚本后,仍然可以在Chrome中访问服务器上的页面。

What could be the cause? 可能是什么原因? Is there a limit of open MYSQLi-connections per browser (per session, that is...) - I couldn't find anything on this... Or is the browser (although FireBug says that the connection has been terminated) nevertheless waiting for a response of the script? 每个浏览器(每个会话,就是...)是否有开放的MYSQLi连接限制?-我在此上找不到任何内容...还是浏览器(尽管FireBug表示连接已终止),但仍在等待对于脚本的响应?

How could I solve this? 我该如何解决? Thanks in advance for any help. 在此先感谢您的帮助。

A PHP script cannot close the client<->server connection except by exiting. PHP脚本无法退出客户端<->服务器连接,除非退出。 doing a Connection: close header doesn't do anything either, as that's for client->server requests, and is the default action anyways, unless the client specifically requests Connection: keep-alive . 执行Connection: close标头也不做任何事情,因为它是针对client-> server请求的,并且无论如何都是默认操作,除非客户端明确要求Connection: keep-alive

The "works in other browsers" behavior you're seeing is generally due to PHP locking the session file while a request is active. 您看到的“在其他浏览器中运行”的行为通常是由于在请求处于活动状态时PHP锁定了会话文件。 The different browsers won't share cookies, so each browser has its OWN separate session. 不同的浏览器不会共享Cookie,因此每个浏览器都有其自己的OWN单独会话。 So while Firefox is tied up with this background request, the session that Chrome is using is completely unaffected. 因此,尽管Firefox与此后台请求绑定在一起,但是Chrome使用的会话完全不受影响。

If you want to continue being able to use FF while it's waiting for this mail script to process, then issue a session_write_close() before you enter the mailer loop. 如果要在等待该邮件脚本处理期间继续使用FF,请在进入邮件程序循环之前发出session_write_close() That will close and unlock the session file, and let you continue using the site in another tab of FF. 这将关闭并解锁会话文件,并让您在FF的另一个选项卡中继续使用该网站。

If you want this script to be truly independent of the browser, then it'll have to pcntl_fork itself into the background. 如果您希望此脚本真正独立于浏览器,则必须将pcntl_fork本身放入后台。 This forked child can handle the processing, completely unattached to a browser, and the original script can exit, allowing the connection to close. 这个分叉的孩子可以处理该处理,而完全不附加到浏览器,并且原始脚本可以退出,从而允许连接关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM