Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a session variable in a function to a helper in codeigniter

Here's what I'm trying to do. This is the function in the controller

public function get_started()
{
    if(test_login($this->session->all_userdata())) {
        $this->load->view('template');
    } else {
        $this->load->view('error');
    }
}

This is the helper

function test_login($sessdata)
{
    if($sessdata->userdata('is_logged_in')) {
        return true;
    } else {
        return false;
    }
}

I have entered is_logged_in as a boolean session variable. However, this doesn't work.

I can't find the error.

like image 298
user1978166 Avatar asked Mar 03 '13 05:03

user1978166


1 Answers

instead of passing session data as parameter to your helper, you could access the session from helper itself, like:

function test_login() {
  $CI = & get_instance();  //get instance, access the CI superobject
  $isLoggedIn = $CI->session->userdata('is_logged_in');
  if( $isLoggedIn ) {
     return TRUE;
  }
  return FALSE;  
}

And controller:

public function get_started(){
   if( test_login() ) {
      $this->load->view('template');
   }
   else {
      $this->load->view('error');
   }
}
like image 104
Sudhir Bastakoti Avatar answered Nov 08 '22 11:11

Sudhir Bastakoti