+2  A: 

you could send a tiny ajax call to your server every 5 seconds. and users that doesn't do this aren't in the room any more

antpaw
There are two ways to optimize this: if you're willing to deal with lots of simultaneous (but idle) connections (something apache+php isn't really good at, but Comet is) choosing long-polling means you'll only be creating a connection every couple of minutes, and you'll get up-to-the-second knowledge of connections dropping. On the flip side, if you go with periodic heartbeats, you'll be establishing and tearing down more connections, but you'll probably have fewer connections open at any given time. Also, your polling interval will determine the resolution at which you discover dropped cons
Frank Farmer
+3  A: 

Sorry, there is no reliable way of doing this, that's the way HTTP was built - it's a "pull" protocol. The only solution I can think of is that "valid" and logged in clients must query the server in a very small interval. If they don't, they're logged out.

naivists
A: 
Tatu Ulmanen
+1  A: 

You answered your own question: if you don't detect a request for new messages from a user over a given length of time (more than a few seconds), then they left the room.

The nature of HTTP dictates that you need to do some AJAX type of communication. If you don't want to listen for the "give me more messages" request (not sure why you wouldn't want to), then build in a heartbeat type communication.

Sam Bisbee
A: 

Maintain a list of active users on the server, as well as the last time they connected to the chat to request new messages.

When a user connects to check for messages update their time.

Whenever your code runs iterate through this list and remove users who haven't connected in too long.

The only failure is that if the number of users in the channel drops to zero, the server wont notice until someone comes back.

preinheimer
A: 

To address your edit, you can ignore client termination by using ignore_user_abort.

Justin Johnson
A: 

Using javascript u can do the following :

<script type="text/javascript">

window.onunload = unloadPage;

function unloadPage()
{
 alert("unload event detected!");
}
</script>

Make the necessary ajax call on the unloadPage() function to ur PHP Script

Undefined
A: 

Request a PHP script that goes a little something like this, with AJAX:

register_shutdown_function("disconnect_current_user");
header('Content-type: multipart/x-mixed-replace; boundary="pulse"');

while(true) {
   echo "--pulse\r\n.\r\n";
   sleep(2);
}

This way, you won't constantly be opening/closing connections.

gnud
A: 

The answers to all the questions asked by the OP are covered in the section in the manual about connection handling:

http://uk3.php.net/manual/en/features.connection-handling.php

No Ajax.

No Javascript.

No keep alives.

C.

symcbean