PHP background Script blocking connection for whole browser

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 …

via PHP Website Development » Search Results » ajax:

PHP background Script blocking connection for whole browser

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.

For more info: PHP background Script blocking connection for whole browser

PHP Website Development » Search Results » ajax

PHP background Script blocking connection for whole browser

Share this post:

Related Posts

Leave a Comment