Sabri Aziri
Sabri Aziri

Reputation: 4184

Codeigniter - get all users session data

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

Upvotes: 8

Views: 88446

Answers (8)

Silver Moon
Silver Moon

Reputation: 1

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

Upvotes: 0

Manish Patil
Manish Patil

Reputation: 21

To Print All Session Data

echo '<pre>'; print_r($this->session);

Upvotes: 0

omega_mi
omega_mi

Reputation: 718

$this->session->userdata('data')->username

Upvotes: 0

Manish
Manish

Reputation: 328

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

Upvotes: 1

Marius Cucuruz
Marius Cucuruz

Reputation: 165

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.

Upvotes: 1

Mohan
Mohan

Reputation: 4829

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;

Upvotes: 26

pgee70
pgee70

Reputation: 3984

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.

Upvotes: 0

stavgian
stavgian

Reputation: 121

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'];

Upvotes: 5

Related Questions