user1978166 user1978166 -4 years ago 50
PHP Question

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.

Answer Source

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');
   }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download