Session variable in Codeigniter Not Working Session variable in Codeigniter Not Working codeigniter codeigniter

Session variable in Codeigniter Not Working


First of All in CI you don't need to use session_start() anywhere. only autoload session in config/autoload.php file.

Remove that session_start().Try to use flashdata. Flashdata is temporary session mostly used in your type of case where We need to redirect user to another page and display some success or error message.

  1. Set flashdata:$this->session->set_flashdata('item', 'value');

  2. get flashdata:$this->session->flashdata('item');

Here is the documentation link http://www.codeigniter.com/user_guide/libraries/sessions.html#flashdata


use autoload applications/config/autoload.php, to add session liblary and remove session_start();

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

Model pass post data vithout varibles, you can do like that:

controller:

public function verification(){     $result = $this->site_model->login();      if ($result){          redirect ('site/index');      } else {          redirect ('site/login');      }}

Model:

public function login(){    $this->db->where('username' , $this->input->post('username'));    $this->db->where('password', $this->input->post('password'));    $query = $this->db->get('users');    if ($query->num_rows()>0)    {        $res = $query->row_array();        $data = array(            'user_name' => $res['username'],            'logged_in' => TRUE,            'validated' => true        );        $this->session->set_userdata($data);        return true;    } else {        return false;    } }


set the session variable in controller..this works for me nice

model

    function Login($email,$pass){         $this->db->where('username' ,$email);         $this->db->where('password',$pass);         $query=$this->db->get('users');         if ($query->num_rows == 1) {               return true;          }           return FALSE;    }

Controller :

        function varification(){            $this->load->model('login_model');    $email=$this->input->post('email');    $pass=$this->input->post('pass');    $success=$this->login_model->Login($email,$pass);    if ($success) {                  $data = array(                       'user_name' => $rows->username,                       'logged_in' => TRUE,                      'validated' => true                 );                $this->session->set_userdata($data);                redirect ('site/index');    } else{ // incorrect id or password             redirect ('site/login');            }    }

now if you echo <?php echo $this->session->userdata('user_name');?> it will show the session username. you can echo it anywhere even in the view.hope this will help you