I am creating a simple real time chat application , so i have to show the chatBuddyList
on the right side of the page.
Currently i have 2 tables for users, tbl_users
(user_id
,name,email...) and tbl_logged_user
(id,user_id
).
on user login i will insert the user_id
to the tbl_logged_users
and in logout i am removing that record.
everything is fine , but the problem is with Logout
. when users clicks on logout
link it will work,but sometime the user may automatically logged out due to session expiration,browser close etc ...
How can i handle such situations ? and what will be the best way to achieve this ?
Thanks.
I am trying to find the best method for this , simply because the exact application is not a real chat based one , i have a table/s with average of
80,000
records. And polling/et is running about 5-10 seconds of time frame.
EDIT
There are some answers saying about session_id
. I believe that its not useful becuase session time out php cannot automatically update the database table unless there is a new request.
I am creating a simple real time chat application , so i have to show the chatBuddyList
on the right side of the page.
Currently i have 2 tables for users, tbl_users
(user_id
,name,email...) and tbl_logged_user
(id,user_id
).
on user login i will insert the user_id
to the tbl_logged_users
and in logout i am removing that record.
everything is fine , but the problem is with Logout
. when users clicks on logout
link it will work,but sometime the user may automatically logged out due to session expiration,browser close etc ...
How can i handle such situations ? and what will be the best way to achieve this ?
Thanks.
I am trying to find the best method for this , simply because the exact application is not a real chat based one , i have a table/s with average of
80,000
records. And polling/et is running about 5-10 seconds of time frame.
EDIT
There are some answers saying about session_id
. I believe that its not useful becuase session time out php cannot automatically update the database table unless there is a new request.
- I've done this before. To catch something like this, it is necessary to write a timestamp with the last contact with the client in the database and test whether that is older than x seconds. – fnkr Commented Mar 11, 2013 at 9:37
-
@fnkr what about
idle
user. – Red Commented Mar 11, 2013 at 9:43 - you can implement the SessionHandlerInterface. In your destroy method you can update the database using the session_id(). – mend3 Commented Oct 31, 2017 at 11:42
6 Answers
Reset to default 3Well, this is not the "usual" way of solving the problem, but I think it is not a bad solution:
You can use a websocket with a tiny Node.js server for example. When a user loads the page with a valid session, it connects to the server (just two lines with javascript). When it disconnects (closes the page) the websocket brokens and the server catches the event.
If the user closes the browser, the socket disconnects. If the user clicks logout, the page reloads and then the socket is not created again (no valid session). The only problem es when the user leaves the browser open for a long time and session expires. Well, adding a timeout in the server would solve this.
If you are creating a chat application try websockets, you won't regret.
This is the session checker code in my chat application.
$CONFIG["app:maxLatency"] is time in seconds. After this time, the client will be logged out if he did not contact the server.
You need a table called users with id (integer), lastseen (timestamp) and sid (session id, text)
Sample table:
id | lastseen | sid
--------------------------------------------------
123 | 2013-03-11 11:00:00 | abcdefg12345
Sample code:
function DeleteSessionByUserId($user_id) {
$user_id = mysql_real_escape_string($user_id);
global $CONFIG;
$sql = "UPDATE users SET sid = '' WHERE id = '".$user_id."'";
$result = mysql_query($sql);
return true;
}
// This will delete all users with expired sessions
function CheckAllSessionsExpired() {
global $CONFIG;
$sql = "SELECT id FROM users WHERE sid != '' AND lastseen < '".date("Y-m-d H:i:s", strtotime("-".$CONFIG["app:maxLatency"]." seconds"))."'";
$result = mysql_query($sql);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
DeleteSessionByUserId($line['id']);
}
return true;
}
// This will update the last seen timestamp in MySQL
function UserSetSeen($user_id) {
$user_id = mysql_real_escape_string($user_id);
global $CONFIG;
$sql = "UPDATE users SET lastseen = '".date("Y-m-d H:i:s")."' WHERE id = '".$user_id."';";
$result = mysql_query($sql);
return true;
}
Store session id in table while logging and keep checking the session id at regular interval, to state user is online, if user closes browser session id wont match then logout the user.
Consider a user logged in, if he was active in the last 5 minutes (or so). That's the way almost every website handles it. You can just register a timestamp every time the user "does" something (i.e. sending a message in your chatbox)
Just add a status to the users table. Update the status to 1 on login and reset to 0 on log out. Then select all results from the users table where status = 1 using AJAX every 5 or 10 seconds. Make a users_online div and display the results.
if you're maintaining sessions in a table, just count the number of active sessions in the table.