CodeIgniter Forums
How to count logged in users? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to count logged in users? (/showthread.php?tid=77400)



How to count logged in users? - valema - 08-27-2020

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?


RE: How to count logged in users? - php_rocs - 08-27-2020

@valema,

When a user logs in do you store (in the database) any flags or data that documents when if if a user is logged in? If so, then all you would need to do is simply query the database to tell you when a user(s) is logged in.


RE: How to count logged in users? - valema - 08-27-2020

@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?


RE: How to count logged in users? - php_rocs - 08-27-2020

@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.


RE: How to count logged in users? - valema - 08-28-2020

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.


RE: How to count logged in users? - InsiteFX - 08-28-2020

Have a helper return the logged in state, you can make the helper global by placing it in
commom.php

PHP Code:
function loggedIn()
{
    return ($_SESSION['loggedIn'] == true) ? true false;


Or whatever you are using for the users state.


RE: How to count logged in users? - php_rocs - 08-28-2020

@valema,

Did you read the documentation (https://codeigniter.com/userguide3/libraries/sessions.html#session-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.


RE: How to count logged in users? - valema - 08-31-2020

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)


RE: How to count logged in users? - InsiteFX - 08-31-2020

To retrieve session data:

PHP Code:
$logged_in $_SESSION['logged_in'];

// or:

$logged_in$session->logged_in;

// or:

$logged_in $session->get('logged_in');

// or session helper

$logged_in session('logged_in');


// Or to get it all in an array

$data $_SESSION;

// or:

$data $session->get(); 

I prefer to use the session helper makes things easy.

PHP Code:
// Session helper

$logged_in session('logged_in');

// or you call the other metods like so

$logged_in session()->get('logged_in');

$logged_in session()->set('logged_in'true); 

The session helper makes it easier to read the code to me.