kitcat
kitcat

Reputation: 157

codeigniter can't access session data in another controller

I want to get data from database using session data as variable . The function is right I think, but it always give null as result. When I display it in view it gets nothing/null. Can anyone help me?

Im defining this code in login controller :

$this->session->set_userdata('id_member', 'id_member');

function DataProfil in controller c_perawat

public function DataProfil(){
        $session_id = $this->session->userdata('id_member');
        $data['klinik']=$this->m_klinik->GetDataKlinik($session_id)->result();
        $data['sidebar']='member/perawat/sidebar';
        $data['content']='member/perawat/pr';
        $this->load->view('member/perawat/main',$data);

    }

My model function

public function GetDataKlinik($session_id){
        $klinik=$this->db->query("SELECT * FROM klinik where id_perawat='$session_id'");
        return $klinik;
    }

Upvotes: 0

Views: 2604

Answers (2)

BEingprabhU
BEingprabhU

Reputation: 1686

Firstly make sure you have autoloaded the session under application/config/autoload.php

$autoload['libraries'] = array('session');

Create an array $session variable before setting it to session

$session_data = array('session_id' => $id); //here you can add any number of session key and values.

Note: Here the $id is your id which you want to set the session.

Then set the $session_data to the session.

$this->session->set_userdata('session_data',$session_data);

To access the session variable

$session_id = $this->session->userdata['session_data']['session_id'];

To unset the session variable,

$this->session->unset_userdata('session_data');

Hope this helps you.

Upvotes: 2

Sofyan Thayf
Sofyan Thayf

Reputation: 1328

Are you sure with this?

$this->session->set_userdata('id_member', 'id_member');

The second parameter must be a value, maybe

$this->session->set_userdata('id_member', $id_member);

Upvotes: 1

Related Questions