开发者

PHP background Script blocking connection for whole browser

开发者 https://www.devze.com 2023-04-13 06:40 出处:网络
I have the 开发者_如何学运维following problem: A PHP Script is called via Ajax. The script itself closes the connection using the following lines

I have the 开发者_如何学运维following problem:

A PHP Script is called via Ajax. 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). Using FireBug, I can see that the connection to the script is directly terminated. 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.

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?

How could I solve this? Thanks in advance for any help.


A PHP script cannot close the client<->server connection except by exiting. 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.

The "works in other browsers" behavior you're seeing is generally due to PHP locking the session file while a request is active. The different browsers won't share cookies, so each browser has its OWN separate session. So while Firefox is tied up with this background request, the session that Chrome is using is completely unaffected.

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. That will close and unlock the session file, and let you continue using the site in another tab of FF.

If you want this script to be truly independent of the browser, then it'll have to pcntl_fork itself into the background. This forked child can handle the processing, completely unattached to a browser, and the original script can exit, allowing the connection to close.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号