Codeigniter - get all users session data

78,024

Solution 1

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;

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
78,024
Sabri Aziri
Author by

Sabri Aziri

A talented, ambitious and self-motivated developer with a strong technical background who possesses self-discipline and the ability to work with "zero" supervision. Strong ability to multitask, react quickly to shifting priorities, passion for new technologies, excellent attention to detail both front and back end and solid web development experience using OOP, PHP, MySQL, PostgreSQL, MongoDB, JavaScript, Angular, HTML, CSS etc.

Updated on July 27, 2022

Comments

  • Sabri Aziri
    Sabri Aziri almost 2 years

    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

  • Sabri Aziri
    Sabri Aziri about 10 years
    Yes this would be an solution but in my case I can not use db :) +1
  • stavgian
    stavgian about 10 years
    You can use session stored in cookkie from the codeigniter config file.
  • omega_mi
    omega_mi almost 5 years
    fetch data from datas in codeigniter session, it worked for me