How to count logged in users? |
Hello. I'm using session to have logged in users, and have set CI (3) to store those in database.
I was thinking (half as a scenario) that I want to replace files in the running system, and want to check if there are anyone logged in to the system. If there is, I would wait and check again - until none is logged in (except maybe me). Is there a way to count logged in users?
@php_rocs thanks for replying. No, just Id and username (in session). I see I can for example have a column in the user table "logged_in" that can be int and 0 means no 1 means yes. Or have a column "last_logged_in" datetime and "last_logged_out". But if user forgets to log out or close browser without logging out?
@valema,
You can approach this many different ways. You could determine if the session is still active and if not then automatically close out the user. You could have a feature that determines if the user is still active and if not then it automatically logs them off. And there are other things you can do as well.
Well. I once tried to find in the sql-select-result from CI_sessions when a user was logged out... but didn't grasp (it). Now I wonder must I use CI session-helper or is it OK to use php's $_SESSION, in order for all needed info to be stored in the CI_sessions table... I also wonder if the "state" (logged_in or not) of a user can be globally recognised, or if it's read with help of users cookies.
Have a helper return the logged in state, you can make the helper global by placing it in
commom.php PHP Code: function loggedIn() Or whatever you are using for the users state. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
@valema,
Did you read the documentation (https://codeigniter.com/userguide3/libra...on-library)? Maybe it can help you to grasp it. The CI session is what I have used in all of my built CI web applications.
Thanks for the answers. I read on CI userguide this example:
$newdata = array( 'username' => 'johndoe', 'email' => '[email protected]', 'logged_in' => TRUE ); $this->session->set_userdata($newdata); How would it be retrieved, 1. in code / not "global" - is it array - by which name? It seems it would be ok with $_SESSION['username'], $_SESSION['email'], $_SESSION['logged_id'], conclusion-1: this "SESSION" could be read only by a certain user... otherwise through database. 2. In the database - how would the three (key/values) be associated - aha they are at one/same row in db right... conclusion-2: the values are associated being at one/same line. I was reading select-result from ci_sessions before logging in and after (logging out) - nothing was added, but I have this error now: A PHP Error was encountered Severity: Warning Message: session_regenerate_id(): Cannot regenerate session id - session is not active Filename: models/X_model.php Line Number: -- After "logging out" Anyway - a first step is to have "logged_in" session variable. My log in function have (reading from user table) $_SESSION["user_name"] = $email; $_SESSION["user_id"] = base64_encode($row->id); log out function have $_SESSION["user_id"] = 0;//test, för det loggades ej ut i Opera 58.0.3135.79 session_unset(); session_destroy(); session_write_close(); setcookie(session_name(), '', 0, '/'); (so it wouldn't fit here to put $_SESSION["logged_in"]=false , because all session/cookies are cleared)
To retrieve session data:
PHP Code: $logged_in = $_SESSION['logged_in']; I prefer to use the session helper makes things easy. PHP Code: // Session helper The session helper makes it easier to read the code to me. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |