简体   繁体   English

获取已登录用户列表 PHP

[英]Get list of logged in users PHP

I am creating a simple real time chat application , so i have to show the chatBuddyList on the right side of the page.我正在创建一个简单的实时聊天应用程序,因此我必须在页面右侧显示chatBuddyList

Currently i have 2 tables for users, tbl_users ( user_id ,name,email...) and tbl_logged_user (id, user_id ).目前我有 2 个用户表, tbl_users ( user_id ,name,email...) 和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.在用户登录时,我会将user_id插入到tbl_logged_users并在注销时删除该记录。

everything is fine , but the problem is with Logout .一切都很好,但问题出在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 ...当用户点击logout链接时它会起作用,但有时用户可能会由于会话过期、浏览器关闭等而自动注销......

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.我正在尝试为此找到最佳方法,仅仅因为确切的应用程序不是基于真正聊天的应用程序,我有一个表,平均有80,000条记录。 And polling/comet is running about 5-10 seconds of time frame.轮询/彗星运行大约 5-10 秒的时间范围。

EDIT编辑

There are some answers saying about session_id .有一些关于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.我相信它没有用,因为会话超时 php 无法自动更新数据库表,除非有新请求。

This is the session checker code in my chat application. 这是我的聊天应用程序中的会话检查器代码。
$CONFIG["app:maxLatency"] is time in seconds. $ CONFIG [“app:maxLatency”]是以秒为单位的时间。 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) 你需要一个名为user的表,其中包含id(整数),lastseen(timestamp)和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;
}

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. 例如,您可以使用带有小型Node.js服务器的websocket。 When a user loads the page with a valid session, it connects to the server (just two lines with javascript). 当用户使用有效会话加载页面时,它会连接到服务器(只有两行使用javascript)。 When it disconnects (closes the page) the websocket brokens and the server catches the event. 当它断开连接(关闭页面)时,websocket brokens和服务器捕获事件。

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). 如果用户单击logout,则页面将重新加载,然后不再创建套接字(无有效会话)。 The only problem comes 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. 如果您正在创建聊天应用程序尝试websockets,您将不会后悔。

记录时将会话ID存储在表中并定期检查会话ID,状态用户在线,如果用户关闭浏览器会话ID不匹配则注销用户。

Consider a user logged in, if he was active in the last 5 minutes (or so). 考虑用户登录,如果他在过去5分钟内(或左右)处于活动状态。 That's the way almost every website handles it. 这几乎是每个网站处理它的方式。 You can just register a timestamp every time the user "does" something (ie 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.登录时将状态更新为 1,注销时将其重置为 0。 Then select all results from the users table where status = 1 using AJAX every 5 or 10 seconds.然后每 5 或 10 秒使用 AJAX 从 users 表中选择 status = 1 的所有结果。 Make a users_online div and display the results.制作一个 users_online div 并显示结果。

如果要在表中维护会话,只需计算表中的活动会话数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM