Welcome Guest, Not a member yet? Register   Sign In
How to count logged in users?
#1

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?
Reply
#2

@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.
Reply
#3

(This post was last modified: 08-27-2020, 02:28 PM by valema.)

@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?
Reply
#4

@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.
Reply
#5

(This post was last modified: 08-28-2020, 05:49 AM by valema.)

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.
Reply
#6

(This post was last modified: 08-28-2020, 06:50 AM by InsiteFX.)

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(This post was last modified: 08-28-2020, 06:49 AM by php_rocs.)

@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.
Reply
#8

(This post was last modified: 08-31-2020, 06:29 AM by valema.)

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)
Reply
#9

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB