Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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?

share|improve this question
Can you tell us about those functions you have tried? – Khaled Jan 29 '12 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 '12 at 15:51

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.

share|improve this answer

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...
  echo "whatever";
share|improve this answer

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.

share|improve this answer
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 '12 at 16:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.