connection drops while script running

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a pretty basic emailing script that sends a relatively small number
(150) of html emails.
 The emails are compiled,  personalised from a mysql db subscribers list,
and sent using mail() - after sending, a small summary html page is sent to
the user with number sent, time taken and a simple navigation choice. Up to
about 100 emails it all works fine - this takes the server about 27 secs .
Any more than that and although the emails are sent ok the connection to the
browser appears terminated - it looks like a 30 sec timeout. The user is
left with a 'connection failed' screen in the browser and the worrying
sensation that the send has failed.

I have the max_execution_time set for 90 secs, (this can be set locally with
a php.ini on an individual directory basis on this server) , no error
messages are sent, and the script obviously completes the emailing (as, on
test, they all arrive). http_connection is set globally on the server to
Keep-Alive .It's apache/linux on the server.

I have an ongoing support ticket with the host company but so far no helpful
response. It feels distincly like a server decision but that's not my area -
I dont know the right questions.

Are there any techniques for keeping the connection alive while a longish
script executes?
Any suggestions?

Re: connection drops while script running

This wrote:
Quoted text here. Click to load it

It sounds like perhaps your browser is timing out, not the server.
Nothing you can do about that from the server side.

So just use the PHP script to create a message template and queue the
information, then do the actual personalization and emailing in a batch job.

It also means your user isn't waiting 5 minutes for a response.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: connection drops while script running

Quoted text here. Click to load it
had a similar problem these days.
Is output_buffering set to a default value( output_buffering!=0 )?
You have to output something so that the browser will not close the
connection after HTTP_KEEP_ALIVE (which i belive is 30s by default
anyway). The connection is closed by the browser,not by the server...

If output_buffering is set, than you have to send that many bytes, so
the browser actually receives something, so it will not consider the
connection as timed-out.

Re: connection drops while script running

So... let me explain the browser mechanism (from i had understand the
other day when i had similar problems)

The browser send a header to the server, with HTTP_KEEP_ALIVE .
Generally this is set to 30 (seconds). I do belive it is a server
config..but 30s should be enough anyway.

The thing is that php uses "output_buffering" which means that when
you call echo "x" from php, the x is not sent imediatly to the
browser, but it is stored temorarly in the "output_buffer.". When
output_buffer grows too large it is flushed to apache, which should
send it to the browser. Funny thing, you can also set an
output_buffering for apache too... So that if output_buffering is 0
and you do and "echo x" that "x" is not sent imediatly to the

From what you see, before anythinng is received by the browser , there
are a few buffers in the way, which you cant control reliably.

Now..from the browser point of view, he has sent HTTP_KEEP_ALIVE with
30 seconds set. This means that "if in 30 seconds i do not receive
anything from the server, kill the connection without notice". If
thebrowser kills the connections the server will not have a chance to
try and flush the buffers. This is why you do not see any data coming
from the server.

The solution is to make some outputs... when i had this problem i
already made some outputs with debug information, but these outputs
were put in a hidden div (i was using AJAX, so the process could be
controlled from the browser-side, in case i needed to stop/restart
it). So, in my case, setting output_buffering to 0 was enough.

If you want to run proceeses in background, i suggest using cron, or
exec(), so you will not limit the browser.
If you do this sort of long-running operations in the browser, then
there are many chances that the browser-server connstion would drop
for eaxmple. And in this case apache will kill the php process which
is running the emailing stuff.

Also, be sure that there is no time limit to your script(if a lime-
limit is in effect, you should see a "timeout exceed" error after the
timeout has expired).
Also , be carefull with session expiring during script execution. Try
to close the session in advance(session_write_close()) and by doing
this you will also be able to view other pages while the emailing
script is running.

There is not one-size fits all when running background srcipts. It
depends on the OS the server is running, the php settings, tha apache

You also could change your script to "automatically" restart every 25
seconds ( send a header of Location: index.php?x=rand() ) , and
continue sending emails from the point it had remained... but i dont
see this as a very elegant solution:)

Quoted text here. Click to load it

----THE END of this transmission----

Re: connection drops while script running

Greetings, This.
In reply to Your message dated Friday, February 15, 2008, 13:43:13,

Quoted text here. Click to load it

Put Your spamming script into CRON and forget about it.


Re: connection drops while script running

AnrDaemon wrote:
Quoted text here. Click to load it

What makes you think he's spamming?  I have several customers who send
out more emails than that at a time.  And they're all done by double
opt-in mailing lists.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline