I have created a chatroom.php and i made target="_blank" that directs the user to the appropriate room whenever a user clicks a room's link. Also, when a user clicks that link i stores the nickname of the user in database, to display the nicknames of the users in that room. However, when a user closes a room's window it still displays that user is online which is not wanted. I need a way that whenever a user closes a room such as chatroom?name=book, i will delete him/her from that room. I hope i made myself clear. It should work like logout.php for certain room. How can i do that?
Thanks
Have a look at the onBeforeClose event.
window.onbeforeclose = function(){
// make a call to PHP saying user is singing off.
};
Alternatively you can use a "idle" timeout (if user hasn't said anything in X minutes, auto-log them off.)
To do this, You need to create a Automatic Task to check for the users which are still their in the CHAT ROOM using SESSION_Checking or ACTIVITY_Monitoring. Once user closes window, The TASK will check and make necessary changes.
To implement in PHP using SESSION Checking:
Every time a user logs in you set $_SESSION['online'] = 1; - the session variable is set only for this user so you don't have to worry for conflicts. But if you want to display this info to all users you should probably save the last action time of your user to the DB. if($_SESSION['online'] == 1;) mysql_query('UPDATE user SET last_activity=NOW() WHERE id='.$user_id); or like that.
So by this way you can keep track of the ONLINE USERS in the CHAT APPLICATION.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With