Welcome Guest, Not a member yet? Register   Sign In
I cannot remove user on logout
#1

[eluser]Unknown[/eluser]
Hi

I am building a logout function, but every time the user is logging out the data is recreated for some reason. Even though i have tried everything to kill the data.

The following methods is what I have tried:

- [ session->unset_userdata('user_id'); ]
- [ session->sess_destroy(); ]
- Delete the entry in the database.
- Deleted the cookie which hold user information.
- Destroyed all cookie and session information.

Right now my code look like this:

Code:
public function logout()
{
  // Load needed classes
  $ci =& get_instance();
  
  $ci->session->unset_userdata('user_id');
  
  $user_id = $ci->session->userdata('user_id');
  if(!empty($user_id))
  {
   return false;
  }
  
  $ci->login_model->del_user_session($ci->session->userdata('session_id'));
  
  delete_cookie('checkout_session');
  setcookie('checkout_session', '', time()-42000, '/');
  
  return true;
}
#2

[eluser]Unknown[/eluser]
I just made a small simple edition of the problem where I still can access protected page even though I have logged out:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller
{

public function index()
{
  $this->load->library(array('session'));
  $this->load->helper(array('url'));
  
  switch($this->uri->segment(1))
  {
   case'login':
    if(isset($_POST['submit_login']))
    {
     $this->session->set_userdata(array('user_id' => 1));
     redirect(base_url(''));
    }
    
    $data = array();
    $this->load->view('test/login', $data);
    break;
   case'logout':
    $this->session->unset_userdata('user_id');
    redirect('login');
    break;
   default:
    if(!$this->session->userdata('user_id')) { redirect('login'); }
    $data = array(
     'meta'  => '',
     'styles' => '',
     'scripts' => '',
     'content' => '<h1>SESSION</h1><pre>'. var_export($this->session->all_userdata(), true) .'</pre>',
    );
    $this->load->view('test/front', $data);
    break;
  }
  
  echo'<pre>';
  print_r($this->session->all_userdata());
  echo'</pre><hr />';
}

}
#3

[eluser]InsiteFX[/eluser]
Code:
setcookie('checkout_session', '', time()-864000);

$ci->session->userdata = array();
$ci->session_destroy();




Theme © iAndrew 2016 - Forum software by © MyBB