最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get list of logged in users PHP - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question edited Mar 11, 2013 at 10:17 Red asked Mar 11, 2013 at 9:33 RedRed 6,39813 gold badges66 silver badges113 bronze badges 3
  • 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
Add a ment  | 

6 Answers 6

Reset to default 3

Well, 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.

发布评论

评论列表(0)

  1. 暂无评论