Users Online, CodeIgniter and Sessions

2019-07-01 13:45发布

问题:

Ok, I am using CodeIgniter as my MVC for a project I am working on with a couple buddies of mine. We are also using CI's Database Session support. With that I do know there are "Who's online" concepts already in existence but none of them really apply to our needs per say. The service we are rolling out is a stand alone SaaS like system however at the same time we are allowing our users to communicate with one another as well. With that we let them share with who they wish specifically information like who's online when, where, etc.. So that said I know CI's sessions catch the IP, as do we when a user logs in.

So what I am wondering is, is it a plausible idea to think I can exploit the session data for what it is, to compare the ips in the session DB against those logged in to in a sense build up my own little "Who's online" specific to people who are "friendly" with one another through our service. If it is a good and potential idea, what would any of you suggest as a means of tackling it and handling it for this type of display? I only ask cause I am not 100% how the sessions via CI work so I am not sure if its going to work in my favor like I am currently imagining.

If my imagination is however running away with me, what would be a good way to tackle this otherwise?

EDIT After logging in on 2 computers I see that the CI session in the DB has 2 with the same IP so my initial thought patter seems to be flawed.

Also thanks for the Down vote, whoever you are, you could have at the least said why in a comment, I am seriously curious to know how to achieve this idea, but I can't get feedback on it? If you know of an article or post here that can help me out then at least point me towards that, cause my searches yield nothing relevant to what Im trying to explain here.

回答1:

Basically, you want to check the _ci_sessions table (or whatever your name is) for records where the last_activity column is within a certain range of the current time (5 minutes, 10 minutes, etc.). With HTTP, there's no great way of knowing the exact moment someone abandoned their session (unless they explicitly logout, and you destroy the session yourself). You could do it with sockets, but that's usually overkill for something like this.

After you get a list of "potential" sessions, you'd still need to loop through each (remember to unserialize the user_data column!) to find the ones that have the correct userdata key/value to constitute a "login" based on your authentication system. I'd do that first to get proof of concept, then find a way to cache the result across a few requests, as it's not request-level accurate anyway, You'd do better without the extra load on every request.