codelearner
codelearner

Reputation: 45

session id in codeigniter

I am using codeigniter and need to get current session id. I have used following code in controller.

function __construct()
{
    parent::__construct();  
        $this->load->library(array('session'));
}

public function index()
{
    $session_id=$this->session->userdata('session_id');

    $data['session_id']=$session_id;
    $this->load->view('cover',$data);
}

If I send print the $session_id in current page or sent it to cover view then it doesn't show the session id. Why it could be?

Upvotes: 0

Views: 1395

Answers (3)

Bang Andre
Bang Andre

Reputation: 541

Session Codeigniter v 3.11

$this->load->library('session');
$test = $this->session;
print_r($test->userdata);

Upvotes: 0

jvk
jvk

Reputation: 2201

First load library in your controller constructor

public function __construct()
{
parent::__construct();  
$this->load->library('session');
}


public function index()
{
    $session_id=$this->session->session_id;

    $data['session_id']=$session_id;
    $this->load->view('cover',$data);
}

Upvotes: 1

Rangka Kacang
Rangka Kacang

Reputation: 327

As per https://codeigniter.com/user_guide/libraries/sessions.html

$name = $_SESSION['name'];
// or:
$name = $this->session->name
// or:
$name = $this->session->userdata('name');

You can also call it directly in your views $this->session->session_id

Upvotes: 1

Related Questions