Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logout in codeigniter

I am trying to design a login/logout page in codeigniter framework.My problem is that when I logout of the web-page I am getting redirected to a login page. When I go back I am getting a page which says:

Document Expired

This document is no longer available.

But when I refresh this page I am getting logged into the system again (o.O)

The following codes contain my constructor and logout functionalities. Please help me to design a perfect login logout page

function __construct()
{
    parent::__construct(); 

    $this->load->model('user_model');    

    $this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
        $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this->output->set_header('Pragma: no-cache');
        $this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    
}

function logout()
{
    $newdata = array(
                'user_name'  =>'',
                'user_email' => '',
                'logged_in' => FALSE,
               );

     $this->session->unset_userdata($newdata);
     $this->session->sess_destroy();

     redirect('default_controller','refresh');
}

I tried find proper logout method but I am not able to.

like image 214
Hacker Rocker Avatar asked Aug 24 '13 21:08

Hacker Rocker


2 Answers

try to be simply like this

function logout()
{
    $user_data = $this->session->all_userdata();
        foreach ($user_data as $key => $value) {
            if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
                $this->session->unset_userdata($key);
            }
        }
    $this->session->sess_destroy();
    redirect('default_controller');
}
like image 160
Nikunj Dhimar Avatar answered Sep 20 '22 11:09

Nikunj Dhimar


don't need to use this line

$this->session->sess_destroy();

like image 45
shams sadek Avatar answered Sep 18 '22 11:09

shams sadek