3
votes

I'm trying to create a simple login/logout feature of a project and my problem is that when I try to logout, the page doesn't redirect to my original login page.

My Admin model goes like this

public function __construct() {
    parent::__construct();
    $this->load->model('AdminModel');
    $this->load->library('session');
    if($this->session->userdata('logged_in')){
        redirect('item/index');
    }
}

public function logout(){
     $this->session->sess_destroy();         
     redirect('admin/index', 'refresh');
 }

This line redirects the page to the home if a user who is logged in tries to access other forbidden pages, however when I logout, it only redirects me to the same user's home page.

if($this->session->userdata('logged_in')){
        redirect('item/index');
    }

Thank you.

1
Try $this->session->unset_userdata(); before sess_destroy - Zeeshan
Try returning the redirect, to like this: return redirect('page') - killstreet

1 Answers

0
votes

Try out the following function

public function logout(){
    if($this->session->userdata('logged_in')){
        foreach ($_SESSION as $key => $value) {
            unset($_SESSION[$key]);
        }

        $this->load->view('admin/index');
    }
}

here the $this->load->view('admin/index'); will load the view you want.