Jump to content

loop members, display online or not


bwakad
 Share

Recommended Posts

I have installed Session Handler Database, but that is more for the Admin side.

I would like to loop through member pages (page name - is the same as - user name), but do not know how to accomplish a "isLoggedIn".

I tried it like this:
$whois = $pages->get("template=user,name={$page->name}");

// I receive correct id's with this so it works
echo $whois->id;

// the next gives error
if($whois->id->isLoggedin()){ echo "yes";}

// the next works but gives online for all...
if($user->isLoggedin($whois->id)){ echo "yes";}

Basically I do not know what to call for in the isLoggedin()...

Does anyone know?

Should be possible since it is PW !

Link to comment
Share on other sites

Oops, sorry I did not mention this. I use a simple foreach to display member profiles.

$members = $pages->find("template=member-profile");
            foreach($members as $page) {

And inside this loop, I want to see who in that list is logged in.

the $page->name equals $user->name...

both use a different template to separate login from displaying.

I thought the loggedin was dependent on the user ID. But I cannot figger out how to display online or not...

Link to comment
Share on other sites

I see - now I understand.

I don't think there is any way to interact with the sessions table using the API, so I think you'll have to use some SQL. Take a look here at the code from the Sessions Process module: https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Session/SessionHandlerDB/ProcessSessionDB.module#L62

I don't have time to write up an example at the moment, but hopefully you can figure it out from there. Otherwise, I will try to help later :)

Link to comment
Share on other sites

I think this should help with what you are trying to achieve:

function isUserOnline($userid, $mins=1){
  $table = SessionHandlerDB::dbTableName;
  $sql = "SELECT COUNT(*) FROM $table " . 
    "WHERE ts > '" . date('Y-m-d H:i:s', (time() - ($mins * 60))) . "' " . 
    "AND user_id=" . $userid . 
    " ORDER BY ts DESC LIMIT 1000";
  $result = wire('db')->query($sql);
  $row = $result->fetch_array();
  return $row[0]==1 ? true : false;
}

$whois = $pages->get("template=user,name={$page->name}");

if(isUserOnline($whois->id)) {
    echo 'true';
}
else{
    echo 'false';
}

With this function you can pass the id of your $whois user to the function and have it return true or false as to whether they are logged in or not. You can pass a second argument to increase the time from 1 minute if you'd prefer. eg:

if(isUserOnline($whois->id, 5)) { // 5minutes

Hope that helps.

Edited by adrian
  • Like 3
Link to comment
Share on other sites

Could not accomplish it.

I am still curious why isLoggedin() only works for $user (is current user).

Isn't there something as a session set when users login ?

Without checking, I would guess it's locked down on purpose. If you could easily access other user's sessions you are reducing security in some way.

What you really want is something like these forums I'm guessing and even that is a bit of a cheat. Because no web server can possibly know if you're still there or not until you interact with it (like click a link or something) the forums store a last active time for each user and the "users online" list shows everyone with a last active time within the last 15 minutes.

It sounds like Adrian's solution should work though if that ts field in the database is updated with every action. I would change 60 to 30 or 15 though personally to get users who actually navigated to a different page more recently.

  • Like 1
Link to comment
Share on other sites

Without checking, I would guess it's locked down on purpose. If you could easily access other user's sessions you are reducing security in some way.

isLoggedIn() doesn't actually have anything to do with whether the user is actually logged in or not - all it does is check if the current user visiting the page is in fact a registered user and not a guest: https://github.com/ryancramerdesign/ProcessWire/blob/6cba9c7c34069325ee8bfc87e34e7f1b5005a18e/wire/core/User.php#L294

So there is no way it could be of any use for looking up whether a specific user is currently logged in or not :)

EDIT: Sorry that might have come across too adamant - of course the user's id is stored in a session variable.

I think Pete's synopsis of how tricky this can be is spot on! 

Edited by adrian
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...