Welcome Guest, Not a member yet? Register   Sign In
finding out how many users are online
#21

[eluser]bretticus[/eluser]
[quote author="North2Alaska" date="1249719397"]
Be careful here. Using the IP address could give you erroneous results. Heres why. Say you are at a WiFi hot spot. When you hit the website the IP that is reported is the IP of the WAN for that hot spot. Now, lets say, that you and your buddy are there. The IP in the session table for each of you sessions will be the same. If you both were logged in and you did the above query, it would only return 1 as the count.[/quote]

Certainly, that's true. Personally, I'd worry more about AOL proxy servers or some other large ISP proxy than two buddies in a coffee shop. One thing is still true about both scenarios, the session ids should be distinct and the activity timestamps relatively current. I suppose if you are going to look at the last 5 minutes or so, you could remove DISTINCT ip_adress. To me the scenario above probably isn't going to hold much sway for this situation.
#22

[eluser]North2Alaska[/eluser]
[quote author="bretticus" date="1249722985"]Certainly, that's true. Personally, I'd worry more about AOL proxy servers or some other large ISP proxy than two buddies in a coffee shop. One thing is still true about both scenarios, the session ids should be distinct and the activity timestamps relatively current. I suppose if you are going to look at the last 5 minutes or so, you could remove DISTINCT ip_adress. To me the scenario above probably isn't going to hold much sway for this situation.[/quote]
Right. There are many scenarios where the session table will not give you the number of active users, no matter how you query it. Part of the original question was [quote author="megabyte" date="1249510739"]Whats the best way to track how many people are logged in to your site. ,,,[/quote] This is problematic using only the session table.

Another scenario; I log in using IE and FireFox and Safari all from the same computer. Now there are three session records and only one actual user. I'm running into this problem myself. I'm trying to figure out how (and where in the code) to enforce only one login per user and a user friendly way to notify the other sessions that they are done and log them out.
#23

[eluser]bretticus[/eluser]
[quote author="North2Alaska" date="1249746007"]
Another scenario; I log in using IE and FireFox and Safari all from the same computer. Now there are three session records and only one actual user. I'm running into this problem myself. I'm trying to figure out how (and where in the code) to enforce only one login per user and a user friendly way to notify the other sessions that they are done and log them out.[/quote]

Good luck! This has been the bane of Web developers from the beginning. If only http weren't a stateless protocol (but then google and facebook wouldn't be possible with all those exra packets most likely.) The fact of the matter is, sessions are your best bet (as I stated before.) If you come up with something better, I will be the first to congratulate you.
#24

[eluser]North2Alaska[/eluser]
[quote author="bretticus" date="1249776896"] If you come up with something better, I will be the first to congratulate you.[/quote]
This may be overly simplistic, but what I have been thinking about is only storing the session_id in the cookie, everything else in the database. I'm using the database anyway, so...

I've added a user_id column to the session table and populate it after login. Now, during the login process I could query for any other record with the same user_id and delete them. This would invalidate any session_id. Now the previous login attempts to access the site, the session_id would not be found and an message could be displayed "Another login has terminated your session" or some such. I just haven't figured out how to keep the cookie to just the session_id and how to store and retrieve the session information to make it available. I'm most likely going to have to overhaul the session class and I just don't know where to begin. :-S But then again, that's what makes coding fun... :-)
#25

[eluser]Zorancho[/eluser]
I think CI sessions are just made for this task, cause you have more control over the code and try to manipulate the Session class the method sess_destroy() or method _sess_gc().
Use the sess_destroy() method to logout the user and in it update the database so you register that the user has logged out, but if the user closes the window or just navigates to the other site then it's better to put the update in the _sess_gc() method cause that will be called to clean up all expired sessions.
This is the closest you will get with this. I think there is not a perfect method for this, even though you can use JavaScript onbeforeunload event and do an ajax call to the database when the user closes the window.




Theme © iAndrew 2016 - Forum software by © MyBB