Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand closed window

Tags:

php

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

like image 499
user893970 Avatar asked Mar 23 '26 21:03

user893970


2 Answers

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.)

like image 80
Brad Christie Avatar answered Mar 26 '26 10:03

Brad Christie


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.

like image 35
djadmin Avatar answered Mar 26 '26 11:03

djadmin