检查用户是否在线,Codeigniter框架 [英] Check if a user is online, Codeigniter framework
问题描述
我想在我的网站中添加在线或离线功能,这是建立在PHP Codeigniter框架之上的。
I want to add online or offline feature in my website, who's built on PHP Codeigniter framework.
我发现这个库 OnlineUsers0.2 。但是,我已经在使用内置的会话类,我想在网上usears利用它。
I found this library OnlineUsers0.2. But, I'm already using built-in session class and I want to take advantage of it on online usears too.
我的想法是插入一个其他字段在 ci_sessions
具有用户ID的表,当我显示用户的信息时,我根据用户ID检查该表,如果它存在于 ci_sessions
表和 last_activity
比用户在线时少5分钟,如果不存在或 last_activity
是比用户离线超过5分钟。
My idea is to insert an other field in ci_sessions
table that has user id, and when I display user's information I do a check to that table based on user id if it's exist on ci_sessions
table and last_activity
is less than 5 minutes than the user is online and if it's not exist or last_activity
is more than 5 minutes than the user is offline.
因此,我的第一个问题是如何将 user_id
字段添加到 ci_sessions
表,这是一个很好的方法。
So, my first question here is how can I add that user_id
field to ci_sessions
table and is it a good way to do that.
我的第二个问题是有更好的方法来做我想要的吗?
And my second question is there a better way to do what I'm looking for?
推荐答案
回答第二个问题,您可以使用javascript websockets轻松地实时完成。您还可以使用相同的逻辑长时间轮询,但如果您的应用程序有很多用户,它不是一个好方法。
Answering your second question, you can use javascript websockets easily for doing this in real time. You can also you long polling using the same logic but its not a good approach if you have many users of your application.
实时在线功能
只需在用户表中创建一个is_online列,并编写一个函数来更新数据库中的is_online状态标志。
Real Time Online function Just create a column 'is_online' in your users table and write a function to update the is_online status flag in the database.
使用 http://socketo.me/ 创建websockets php服务器或 https://github.com/nekudo/php-websocket 。
Create your websockets php server using http://socketo.me/ or https://github.com/nekudo/php-websocket . Here your server logic will reside i.e if user comes online update the online flag and vice versa.
在javascript中使用websockets,你可以简单地挂钩onopen和onclose事件来触发服务器端功能。当用户点击任何页面只需更新数据库中的状态。
In javascript using websockets, you can simply hook onopen and onclose event to do trigger server side functions. As the user hits any of your page simply update the status in database.
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://localhost:9998/echo");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
进一步参考 http://www.tutorialspoint.com/html5/html5_websocket.htm
这篇关于检查用户是否在线,Codeigniter框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!