Codeigniter - get all users session data
Asked Answered
A

8

8

I need to get all sessions data and take some actions upon them, is there any possible way to get them, I found how to solve it in php but codeigniter use's it own custom sessions library.

Native php

$_SESSION

How can I do it in codeigniter

Academia answered 3/3, 2014 at 14:48 Comment(4)
This will help you $this->session->all_userdata();. Load session library before this snippet.Blair
is there any specific data that you want to get that is not available in the codeigniter session class?Vinegarish
@RahilWazir $this->session->all_userdata(); does not help because it has only single array for each of users, what I am try to do e.x Let say i have 2 users logged in into my sistem user A & B and my issue is how do I get session datas of user B from user A. Am i clear enough and thanks for your helpAcademia
@sabri You can create a column status in your user table with default value of 0 and when user logged in execute query and change status value to 1. Query all users whos status is 1 and you will get the data from users whos login. Remember set it back to 0 after logout.Blair
S
26

To get all session data you can use $this->session->all_userdata();

To print all your session variable use this line anywhere in your code :

echo '<pre>'; print_r($this->session->all_userdata());exit;
Schipperke answered 4/3, 2014 at 8:2 Comment(0)
K
5

You can use db session. So data will be stored to the database. See Saving Session Data to a Database

If you have no database support just use cookie and get the data with

$username = 'John Doe';
$this->session->set_userdata('username',$username);
$var = $this->session->userdata;
echo $var['username'];
Knuckleduster answered 3/3, 2014 at 15:18 Comment(2)
Yes this would be an solution but in my case I can not use db :) +1Academia
You can use session stored in cookkie from the codeigniter config file.Knuckleduster
P
1

To get all users session data.First we need to initialize the Session class manually in our controller constructor use following code.

$this->load->library('session');

Then we use following code.

$this->session->all_userdata();

Above function return an associative array like the following.

Array
( [session_id] => 4a5b5dca45708ab1a84364eeb455j007 [ip_address] => 127.0.0.1 [user_agent] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; [last_activity] => 1102132923
)

For complete Ci Session tutorial. Please referrer following link

Paget answered 4/10, 2015 at 5:50 Comment(0)
D
1

If you want to see everything the way I've done it is to cast $this->session as array then print_r it.

        $this->load->library('session');
        echo "<pre>". print_r((array)$this->session, true) ."</pre>";

Hope this helps.

Doro answered 1/4, 2016 at 18:34 Comment(0)
V
0

If you know how to do this in PHP why not implement that, and not use the codeigniter session class? my experience is that the CI session class breaks pretty badly with ajax calls and proxy servers, so it is best avoided.

Virgenvirgie answered 8/3, 2014 at 9:30 Comment(0)
I
0
$this->session->userdata('data')->username
Indeed answered 15/6, 2019 at 19:5 Comment(1)
fetch data from datas in codeigniter session, it worked for meIndeed
O
0

To Print All Session Data

echo '<pre>'; print_r($this->session);
Otter answered 23/8, 2020 at 7:9 Comment(0)
G
0

I needed similar thing to view all active sessions

var_dump($this->session->all_userdata()); 

above code did the job. For details go to the below link

https://codeigniter.com/userguide3/libraries/sessions.html#CI_Session::all_userdata

Gloriole answered 14/8, 2021 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.