Is user online/logged in?

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I'm trying to find a simple solution to seeing if users are online. One way is yes in the DB table when they login and change to no when they logout, but that doesnt work when people close their browsers or clear data etc.

So.. is it possible to access server session values without the unique session ID?
When a user on a site gets a session allocated to them, for example

<?php
$_SESSION
['username']="greg";
?>

Is there a way to find if there is a session with the value of "greg".
"greg" would of course be unique as only one instance of a username is allowed.

Or is there a better method?
Perhaps getting the unique session id when it is created, storing it in the users db row and checking if that session id is on the server.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Technically, if you are on your own server, you could access the files that contain the session data.

IMO it is better to use the DB for this. Add a timestamp field for "last on", as on a membership site, each page should be checking the DB to validate permissions anyhow. set this to updates this value, then when you want to check who is online, do a search in table for users whose time stamp is within the last, say 5 minutes.

-Greg

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Greg K wrote:
Add a timestamp field for "last on"

Hmm

That with the login/logout I mentioned in my first post might make a decent setup.
Still have an "active" field in the db, user logs in is a yes, user logs out is a no, that way if a user is definately logged out I can say so.
Then also, have a timestamp, and if say a user has not done anything for 10 mins they will be listed as inactive for xx mins, then after perhaps 30 mins of inactivity I could just list them as offline.

Greg K wrote:
each page should be checking the DB to validate permissions anyhow

yes, I have an include login check. some pages they have to be logged in and are redirected if not and some just serve member specific or public data depending on their status.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I like the idea of an active field so that if they manually log off it lists that.

What I would do then is also in the part where you update the users time stamp, do a second call that will set Active="N" where last ativity > 30 minutes.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Greg K wrote:
I like the idea of an active field so that if they manually log off it lists that.

What I would do then is also in the part where you update the users time stamp, do a second call that will set Active="N" where last ativity > 30 minutes.


Problem with that is they might not actually be logged out, just inactive, which is what the active field is for. N will allow me to state "not logged in" and dont even bother with the timestamp as they have manually logged out, whereas "Y" I will have to use the timestamp with IF >30mins echo user not logged in.

It's a "fake" logged out notice really, and if the user becomes active again, timestamp <30 and therefore "logged in", which of course would not work if I set Active to "N" after timestamp > 30.

So the timestamp will only ever come into play if active="Y".
If I set Active to "N" after timestamp > 30 then I have stopped the functionality of the timestamp should they become inactive again in that same (logged in) session, and of course change from active to inactive numerous times.

Thanks for the thoughts, it's put me on a good track and a decent setup!

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

greg's picture

He has: 1,581 posts

Joined: Nov 2005

decibel.places wrote:
You can use $_SESSION and isset

http://www.php-mysql-tutorial.com/user-authentication/basic-authentication.php


That will only get any session data relevant to the user who access the page with that PHP on it.
Each person who accesses the site/server is given a unique id to each of the sessions relevant to them. So that wouldn't get all sessions for all users, just all sessions for that user.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.