php - How to build codeigniter login page and session with one controller -


hi. having problem sessions, new codeigniter, please patient me. here's index function:

function index() {     $username;     $pword; //assume these 2 variables has valid , correct value     if($this->coremodel->authenticate($username, $pword)){         $user = $this->coremodel->authenticate($username, $pword);         foreach($user->result() $key) {                 $this->session->set_userdata(array(                                 'logged_in' => true,                                 'cl_userid' => $key->cl_userid,                                 'cl_username' => $key->cl_username,                                 'cl_roles' => $key->cl_roles));                 if($this->session->userdata('cl_roles') == 1){                     header("location: ".base_url()."controller/homepage");                   } else {                     $this->load->view('loginpage',$this->data);                 }          }      } else {                                     //$this->data['msg'] = "wrong username/password";         $this->load->view('loginpage',$this->data);     } } function kickifnotinsession(){     if ($this->session->userdata('logged_in') == false)     {         redirect('controller');      } } 

if enter valid username , password, works fine, , page goes homepage. in homepage, echo id , username. in homepage, have tabs menu. in of pages, echo id , username. have added kickifnotinsession() function in of functions, example:

function test() {     $userid = $this->session->userdata("logged_in");     $this->kickifnotinsession();     $this->load->view("test");   } 

also, have autoload.php:

$autoload['helper'] = array('url'); $autoload['libraries'] = array('database','session'); 

after logging in, clicked tabs , reason works fine. echoes id , username, after minute or 2 when click tab, kicks me out of sessions , directs me login page. wonder how happened. missing something?

check sess_expiration parameter in application/config/config.php file. may set incorrect.

'sess_expiration' = number of seconds want session last. default sessions last 7200 seconds (two hours). set 0 no expiration.


Comments