Welcome Guest, Not a member yet? Register   Sign In
Custom logout function not working
#1

[eluser]RyanH[/eluser]
I've been trying to figure out why my custom logout function isn't working and I can't seem to figure it out. I realize that my code is probably not the best way to go about doing things, so I'm open to suggestions. I'm still learning my way around CodeIgniter. My code is below so if anybody has any advice, I'd appreciate it. Thanks.

Code:
// Run the validation and if it's not successful return them to the login page
        // If the validation is successful, process the login
        
        if($this->validation->run() == FALSE)
        {
            $this->load->view('login_view');
        } else {            
            $data = array(
                'email' => $this->validation->email,
                'password' => $this->validation->password
                );
                
            $this->db->where(array('email' => $this->validation->email, 'password' => $this->validation->password));
            $query = $this->db->get('users');
            
            if($query->num_rows() > 0)
            {
            /*    $newData = array(
                    'email' => $this->validation->email,
                    'logged_in' => 1
                    );

                $this->session->set_userdata('email', $this->validation->email); */
                
                $this->load->view('login_complete');
            } else {
                $this->load->view('login_view');
            }
        }
    }
    
    function logout()
    {
        $logout = $this->session->sess_destroy();
        if($logout)
        {
            $this->load->view('home_view');
        } else {
            echo 'Error logging out';
        }
    }

This is the code on the temporary page that loads if you're login was successful (located at the very top of the page):
Code:
if(!$this->session->userdata('session_id'))
        {
            redirect('login', 'location');
         } else {
                echo 'Welcome ' . anchor('login/logout', 'Logout');
        }
#2

[eluser]adamp1[/eluser]
I know right away why it doesn't work, change
Code:
$logout = $this->session->sess_destroy();

To this
Code:
$logout = $this->session->sess_destroy();
$this->session->sess_create();

This is a common mistake everyone makes. Not 100% sure why it doesn't work the other way, I did know but forgotten now. I believe its got something to do with the cookie not being erased till next page load or something.
#3

[eluser]RyanH[/eluser]
I changed it the way you suggested and unfortunately it's still throwing the error for "Error logging out" that I set it to say if it had issues. I really don't understand why I'm having so many issues with this.
#4

[eluser]adamp1[/eluser]
First of all this code isn't very good
Code:
$logout = $this->session->sess_destroy();
if($logout)
{
   $this->load->view('home_view');
} else {
   echo 'Error logging out';
}

That will always return 'Error logging out' since sess_destroy returns NULL. Never TRUE.

Are you testing if they are logged in by asking if there is a session_id in their session? If so that's not the best way I would say since every time someone goes to your site, they get this created automatically.

Can you post your full login code, and also the code you use to check if someone is logged in? Or is that it what you have posted?
#5

[eluser]RyanH[/eluser]
That's pretty much all of the code. What I'm looking to do is simply log someone out, not even check to see if they're logged in, that shouldn't be that hard to do on each page. I guess I'm going about it the wrong way.

I'd actually like to use Erkanaauth but I haven't been able to get it to work so I'm trying to use my own. I don't need anything fancy, either.
#6

[eluser]adamp1[/eluser]
Well Erkanaauth is one of the simplest authentication libraries. If you cannot get that to work then it might suggest there's an issue with your setup.

What went wrong when you tried Erkanaauth?
#7

[eluser]RyanH[/eluser]
It would make sense if I wasn't setting it up correctly. I guess I'm confused if I need to use the MY_Controller and the auth files or not. I've been looking for working examples and I can't seem to find any that help me out. I really don't understand why I'm having so many issues with authentication and session in general, I haven't had this much trouble CI in the past with anything. I don't need the validation controller, I just want to be able to log someone in, log them out, and restrict what pages they can view unless they're logged in. Should be pretty simple, I would think.




Theme © iAndrew 2016 - Forum software by © MyBB