Welcome Guest, Not a member yet? Register   Sign In
session problems - not loggin out *** FIXED *** Code Provided
#1

[eluser]Bainzy[/eluser]
Hi everyone,

I have run into a bit of a problem with sessions today, im sure its propbs my code but when i call the session to be destroyed its not clearing the sessions, i am still logged in :S

Here is my code from loggin in :

First my controller :

Code:
function submit_login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('login');
        }
        else
        {
        $username = $this->input->post('username');
        $password = dohash($this->input->post('password'), 'md5');
        
        // Returns userid if matched, false if not
        $results = $this->MUsers->submit_login($username, $password);
        
        if($results==false)
        {
            $this->load->view('login');
        }
        else
        {            
            redirect('forums/topics');
        }
        }
    }

and here is my model


Code:
function submit_login($username, $password)
    {

        $q = $this->db->get_where('users', array('Username'=>$username, 'Password'=>$password));
        
        if($q->num_rows() == 0)
        {
            return false;
        }
        else
        {
            $results = $q->result();
            
            $data = array (
                'userid' => $results[0]->UserID,
                'username' => $results[0]->Username,
                'logged_in' => TRUE,
            );
            
            $this->session->set_userdata($data);
            
            return true;
        }
    }

I am checking if the user is logged in by using this code :

Code:
if($this->session->userdata('logged_in') == FALSE)
    {
    // Login link here
    }
    else
    {
    // Welcome code here
    }



and my logout code in my controller
Code:
function logout()
    {
        $this->session->unset_userdata(array('logged_in'=>''));
        redirect('forums/topics');
    }

Can anyone see where i am going wrong ?

Regards
Chris
#2

[eluser]Rob Gordijn[/eluser]
read your code, looks correct.
maybe you can try: $this->session->unset_userdata(array('logged_in'=>FALSE));
so use FALSE instead of ''
didn't lookup the source for session yet, but maye this wil fix it.
#3

[eluser]Bainzy[/eluser]
no i had already tried this several different ways i have even used sess_destroy() but even that is no clearning my sessions :S i think its a problem with the way they are being registered.
#4

[eluser]Bainzy[/eluser]
Hey everyone,

I have fixed the problem with a bit of trial and error, the problem actualy came from the logout link, i was not calling the correct controller so it was not performing any action.

I did however modify the code to make it slightly easier to work with and add things to. So here is the code as it stands now :

Model :

Code:
function submit_login($username, $password)
    {

        $q = $this->db->get_where('users', array('Username'=>$username, 'Password'=>$password));
        
        if($q->num_rows() > 0)
        {
            foreach ($q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
       $q->free_result();
       return $data;

    }

Controller :

Code:
function submit_login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('login');
        }
        else
        {
        $username = $this->input->post('username');
        $password = dohash($this->input->post('password'), 'md5');
        
        // Returns userid if matched, false if not
        $results = $this->MUsers->submit_login($username, $password);
        
        if($results==false)
        {
            $this->load->view('login');
        }
        else
        {
            foreach($results as $row)
            {
                $data = array(
                    'logged_in' => true,
                    'username' => $row['Username'],
                    );
            }
            $this->session->set_userdata($data);
            redirect('forums/topics');
        }
        }
    }

I hope this may be some use to someone Smile

Regards
Chris
#5

[eluser]Rob Gordijn[/eluser]
good that your problem is solved Smile




Theme © iAndrew 2016 - Forum software by © MyBB