Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

http://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?

share|improve this question
1  
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
add comment

2 Answers 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.

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.