4

I've posted the following question to SO, but thought there might be a server based solution.

https://stackoverflow.com/questions/9053964/php-script-with-sleep-does-not-exit-on-connection-close

I'm running an Ubuntu VPS to run this script, and I'm trying to get the script to die when the user closes the window/tab of his browser.

There are several PHP based functions to see if the connection is still open, but none works (trust me, tested them all).

Any creative ideas on how I can do this through the server maybe?

2
  • 1
    Can you tell us about those functions you have tried?
    – Khaled
    Jan 29, 2012 at 15:49
  • @Khaled Sure, PHP has the following functions: connection_aborted(), ignore_user_abort() and connection_status(). first and last one should indicate if the connection was aborted, supposedly works when a user clicks the 'stop' button, anyways does not work when the user closes the window. the ignore_user_abort() setting (when set to 'false') should exit a script when the connection is closed, but again here, does not work..
    – user838437
    Jan 29, 2012 at 15:51

3 Answers 3

2

When you were trying out these methods, did you force PHP to flush the buffer? Until something is written from the script, PHP/Apache won't be able to see if the client is still connected. Keep in mind, PHP is not directly communicating with the client--its communicating with Apache.

The comments are the bottom of the connection_aborted PHP manual contain several strategies.

1

https://stackoverflow.com/questions/23530820/php-connection-aborted-does-not-always-works/29401757#29401757

TCP requires that ALL sent packets be acknowledged by the client and therefore the server should detect this as a send timeout at the very least...

session_write_close();//to make flush work
while (connection_status() !== 0) {//this will work if the connection is properly shutdown
                                   //or if it is simply disconnected...
  sleep(1);
  echo "whatever";
  ob_flush();
  flush();
}
0

If the web browser isn't closing the connection, your script probably keeps running until the connection times out or it hit's PHP's max execute time.

The best thing I can think of would be to use AJAX to regularly "ping" the server and stop when it hasn't received those pings for awhile. Or every time it receives a ping, give the PHP script 5 more seconds to run.

Or you could try using AJAX to send a message to the server when the user closes the window, but I think that ability is disabled in some newer browsers because it's often used for annoying popup ads and such.

1
  • Thanks for your answer, unfortunately I cannot use any client side scripting like JS. I need to use only PHP. I also need to measure how long exactly the user had the window open, so waiting for a certain amount of time isn't a good option for me..
    – user838437
    Jan 29, 2012 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .