bwakad Posted August 23, 2014 Share Posted August 23, 2014 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 More sharing options...
adrian Posted August 23, 2014 Share Posted August 23, 2014 Are you trying to test if the logged in user is the person whose page it is? if($user->name == $page->name) Link to comment Share on other sites More sharing options...
bwakad Posted August 23, 2014 Author Share Posted August 23, 2014 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 More sharing options...
adrian Posted August 23, 2014 Share Posted August 23, 2014 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 More sharing options...
adrian Posted August 23, 2014 Share Posted August 23, 2014 I didn't have to bother. Check out this answer: https://processwire.com/talk/topic/5318-select-online-users/?p=51205 Link to comment Share on other sites More sharing options...
bwakad Posted August 24, 2014 Author Share Posted August 24, 2014 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 ? Link to comment Share on other sites More sharing options...
adrian Posted August 24, 2014 Share Posted August 24, 2014 (edited) 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 August 24, 2014 by adrian 3 Link to comment Share on other sites More sharing options...
Pete Posted August 24, 2014 Share Posted August 24, 2014 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. 1 Link to comment Share on other sites More sharing options...
adrian Posted August 24, 2014 Share Posted August 24, 2014 (edited) 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 August 24, 2014 by adrian Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now