Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Connection between client and server

8 views
Skip to first unread message

bit-n...@hotmail.com

unread,
Jul 24, 2016, 3:52:04 AM7/24/16
to
If I have a page which say, echo's something, and then does stuff on the server side, eg. putting something inside a database, say - I was thinking of *FIRST* doing the echo'ing, and THEN doing the "stuff" - which obviously means that if the server crashes between the echo and the stuff, the task won't get done. But I'm also wondering what happens if someone clicks off that tab or closes the browser as soon as they see the text (whatever's been echo'd) - will that kill the process on the server side? Before it's completed whatever it was supposed to do? How do I prevent this?


Thanks.

J.O. Aho

unread,
Jul 24, 2016, 4:44:14 AM7/24/16
to
On 07/24/16 09:51, bit-n...@hotmail.com wrote:
> If I have a page which say, echo's something, and then does stuff on the server side, eg. putting something inside a database, say - I was thinking of *FIRST* doing the echo'ing, and THEN doing the "stuff" - which obviously means that if the server crashes between the echo and the stuff, the task won't get done. But I'm also wondering what happens if someone clicks off that tab or closes the browser as soon as they see the text (whatever's been echo'd) - will that kill the process on the server side? Before it's completed whatever it was supposed to do? How do I prevent this?

The process should in most cases execute to the end, but still the best
practice is to execute things first and then give the output, as this
way you can tell that something went wrong and ask the user to redo the
post.

--

//Aho

Gordon Burditt

unread,
Jul 24, 2016, 5:14:42 AM7/24/16
to
> If I have a page which say, echo's something, and then does stuff
> on the server side, eg. putting something inside a database, say -
> I was thinking of *FIRST* doing the echo'ing, and THEN doing the
> "stuff" - which obviously means that if the server crashes between
> the echo and the stuff, the task won't get done. But I'm also
> wondering what happens if someone clicks off that tab or closes the
> browser as soon as they see the text (whatever's been echo'd) -

Check out the PHP ignore_user_abort(true) function. You may also
need set_time_limit(). And flush() may be useful to get a "please
wait" message out BEFORE the time-consuming stuff starts instead
of after.

DON'T make that message "thank you for purchasing blah blah blah".
(You haven't even started to charge the credit card yet, anyway.)
Make it more like "processing your order, please wait".

> will that kill the process on the server side? Before it's completed
> whatever it was supposed to do? How do I prevent this?

Then, when it finishes, (optionally: turn off ignore_user_abort()
after setting up the order and before generating the receipt),
output a receipt for the order and invite the user to save it.

The receipt should contain such things as an order number, user's
name / account, the date, the amount charged, optionally what was
ordered, and a link to where to see order status. Or maybe it's
just "Your message has been successfully posted".

Don't tell the user it's done until it's done. You can never know
when your server will suffer a direct lightning hit, a bomb, or an
18-wheeler will knock down the pole containing your internet
connection. Of course, lightning hits and bombs can kill orders
entered the previous week (or perhaps decade). That's one reason
for real-time replicated databases on different continents.

I don't think *closing the browser* or *navigating away from the page*
by itself causes an abort. What causes an abort happens when the
browser discovers it can't deliver more data to the browser.

Either way, the user won't see the output. But you do get to finish
registering the transaction in a database.

So:

echo "Preparing to Frombulate ..\n";
flush();
/* ignore_user_abort(true); uncomment to avoid script halting */
/* user closes browser here or hits STOP */

... Do the frombulation ...
... This takes half a minute ...

echo "Halfway through the Frombulation ...\n";
flush(); <---- KABOOM!! Script aborts here.

... Clean up the frombulation ...
echo "Frombulation complete.\n";
exit;

The script doesn't stop until it generates and sends output *after*
the browser gets closed. If the output is short, and you don't
flush() to force it to be sent, it might not be sent until later.


0 new messages