Question

I'm trying to figure out a way to display if a user (not self) is currently logged in. Currently this is for the edit_user page and only intended to be useful as an admin for the time being, but that will likely expand as I develop. I was thinking something along the lines of

if( $user->ion_auth->logged_in() ) {
    echo "<span class='active'>$user->username</span>";
}

But that doesn't work. Any ideas?

I found my answer and supplied it below, thank you for the other answers and sparking some ideas.

Was it helpful?

Solution 2

I liked the input and it got me thinking. So here is the solution I decided to go with. Instead of adding insert or update statements, I decided to use the serialized session data already in the database. This also takes advantage of the existing functionality of ci and ion-auth without modifying them. So here is the code if someone else wants to use it:

// character length of the userid
$userID_Length = strlen( $user->id );

// set a time to look at for validity
$now = time() - ( 10 * 60 ); // (minutes x seconds)

// query statement
$statement = "SELECT session_id FROM ci_sessions WHERE last_activity >= $now AND user_data LIKE '%s:7:\"user_id\";s:$userID_Length:\"$user->id\";%'";
// run query
$query = $this->db->query( $statement );

// check results of query
if( $query->num_rows() > 0 )
{
    // positive result
}
else
{
    // negative result
}

OTHER TIPS

The "active" field is used so you can turn on/off the ability for a user to log in. If you look in the database, you'll see all of your users you've created have "active=1". This field does not tell you whether a user is logged in or not. The only sort of field that can give you some indication is the "last_login" field, which tells you when they last logged in. However this is not going to tell you if they are currently logged in (basically you are wanting to know if they still have an active session, and there's no way of knowing this since you are not on that user's pc).

The only way that I know of to more accurately track if a user is currently logged in, is to have the database keep track of when the last time the user accessed any page. If the last time a user accessed a page is too long ago, then you know that user is no longer logged in. I wouldn't recommend this approach, since there essentially is a database write for every request/refresh on your site by this user. This could be problematic for your database/server load, especially if you get busy with users.

So what are some more reasonable choices that you have?

  1. Just go by the last login to get some feel of who's logged in.
  2. Run a pre-system or pre-controller hook to update the user's table if the user is still logged in, but write this to the database less frequently, perhaps on the hour (+ some random minutes to disperse writes a little more). You'll need to add a new field to the user's table for tracking this.
  3. Google Analytics approximates a number of active users on your site. However this only gives you a total and not specifics on who these users actually are.

Ion auth already has a column last_login.Add another column last_logout to users table.(unix time)

Now use query to get logged in users

SELECT `user_name` From `users` WHERE `login_time` > `logout_time`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top