Welcome Guest, Not a member yet? Register   Sign In
$this->session->sess_destroy() not working
#1

[eluser]dennismonsewicz[/eluser]
I have written my admin portion of my site and for some reason calling the $this->session->sess_destroy() method does not work... I have to physically tell my code to unset the data I have in the session before it will work.

Any idea as to why this would happen?

My Auth Library
Code:
<?php
    Class Auth {
        var $CI;
        
        function Auth() {
            $this->CI =& get_instance();
            $this->CI->load->database();
        }
        
        function process_login($login = NULL) {
            if(!isset($login))
                return FALSE;
                
            if(count($login) != 2)
                return FALSE;
                
            $username = $login[0];
            $password = md5($login[1]);
            
            $query = $this->CI->db->get_where('users', array('username' => $username, 'password' => $password));
            $results = $query->result();
            
            if($query->num_rows() == 1) {
                $this->CI->session->set_userdata('username', $username);
                $this->CI->session->set_userdata('is_admin', $results[0]->is_admin);
                return TRUE;
            } else {
                return FALSE;
            }
        }
        
        function logged_in() {
            if($this->CI->session->userdata('username') == false) return false;
            else return true;
        }
        
        function restrict($logged_out = false) {
            if($logged_out && $this->logged_in())
                redirec('/admin/main/index');
                
            if(!$logged_out && !$this->logged_in()) {
                redirect('/admin/login');
            }
        }
     }

Login#logout Controller
Code:
<?php
class Login extends Controller {
    function Login() {
        parent::Controller();
    }
    
    function index() {
        if(!$this->auth->logged_in()) {
            $this->session->set_flashdata("message",  popup_msg("Please Login Below"));
            $this->load->view('admin/login_view');
        } else {
            redirect('/admin/main/index');
        }
    }
    
    function authorize() {
        if($this->input->post('_login') == '1') {
            $login = array($this->input->post('username'), $this->input->post('password'));
            if($this->auth->process_login($login)) {
                redirect('/admin/main/index');
            } else {
                redirect('/admin/login');
            }
        }
    }
    
    function logout() {
        // $this->session->unset_userdata('username');
        //         $this->session->unset_userdata('is_admin');
        $this->session->sess_destroy();
        $this->session->set_flashdata("message",  popup_msg("You have logged out"));
        redirect('/admin/login');    
    }
}
?>

For some reason the code just keeps redirecting to /admin/main/index instead of going back to the login screen
#2

[eluser]WanWizard[/eluser]
calling sess_destroy() deletes the session cookie, and the session record if database sessions are used. It doesn't delete the userdata loaded at the beginning of the session.

You want want that, extend the session library:
Code:
class MY_Session extends CI_Session
{
    function MY_Session()
    {
        parent::CI_Session();
    }

    function sess_destroy()
    {
        parent::sess_destroy();
        $this->userdata  = array();
    }
}
#3

[eluser]kkristo[/eluser]
You need call $this->session->unset_userdata('var');
Or don't set flash_data after destroy. Call only sess_destory and thats all
#4

[eluser]dennismonsewicz[/eluser]
Doesn't that seem like a bug though? I should be able to destroy the session then immediately afterwards set a flash data message and it work.
#5

[eluser]WanWizard[/eluser]
I don't know what Ellislab's intentions were when they wrote it, so I can't say if this is a bug or not.

But I think it qualifies as a feature request though. It's been reported before...
#6

[eluser]dennismonsewicz[/eluser]
Hmmm alright I shall try to ask for it be added as a feature.

I appreciate all of the help! I will just unset all of the session userdata individually for now.




Theme © iAndrew 2016 - Forum software by © MyBB