Welcome Guest, Not a member yet? Register   Sign In
unable to destroy session
#1

[eluser]zqkun[/eluser]
What I want to implement is a very simple login page, if user login successfully, then redirect to main page, else remain login page.

I have 2 controllers named login and main, and I also have 1 model named model_main.

Here is how it works:
When user click the login button, the login/login_send will be called,
if login sucess, it will redirect to the main page(
Code:
redirect('main');
).
if user press logout, it will destroy session and redirect to login page(
Code:
redirect('login');
).

When I first time login and then logout, it's fine, no problem, even I refresh the login page, it still shows correctly.
If I try to login second time, and then logout, it will show the login page again. However, at this point, if I refresh the page, it will jump to the main page. After that, no matter how many times I pressed the 'logout' button, it doesn't work at all.


Below are my codes:
controller/login.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller{
function __construct() {
  parent::__construct();
  $this->load->model('model_login');
}

function index()
{
  if ($this->model_login->is_logged_in())
   redirect('main');
  else
  {
   // load login page
   $data['main'] = 'view_login';
   $data['style'] = 'style_login';
   $this->load->view('template', $data);  
  }
}


function login_send()
{
  $this->form_validation->set_rules('username', 'Username', 'trim|required');
  $this->form_validation->set_rules('password', 'Password', 'trim|required');
  
  if ($this->form_validation->run() == FALSE)
   $this->index();
  else
  {
   if ( $this->model_login->validate_user() )
   {
    $user_session_data = array(
     'username' => $this->input->post('username'),
     'is_logged_in' => 1
    );
    $this->session->set_userdata($user_session_data);  
    redirect('main');
   }
   else redirect('login');
  }  
}// end function login_send()


function logout()
{
  if ($this->model_login->is_logged_in())
  {
   $this->session->sess_destroy();
   //$this->session->set_userdata(array('username' => '', 'is_logged_in' => 0));
   log_message('debug', 'Some variable was correctly set');
  }
  redirect('login','refresh');
}

}// end class Login
?>


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

class Main extends CI_Controller{

function __construct()
{
  parent::__construct();
  
  $this->load->model('model_login');
  
  if (!$this->model_login->is_logged_in())
   redirect('errors/test2');
}

function index()
{
  $data['main'] = 'view_main';
  $data['style'] = 'style_main';
  $this->load->view('template', $data);
}
}


models/model_login.php
Code:
<?php
class Model_login extends CI_MOdel{

function _isUserExist($username, $password)
{
  
  $options = array(
   'UserName' => $username,
   'Password' => $password
  );
  $query = $this->db->get_where('userinfo', $options);
  return $query->num_rows() > 0;
}

function validate_user()
{
  $username = $this->input->post('username');
  $password = $this->input->post('password');
  $role = $this->input->post('role');

  return ($this->_isUserExist($username, $password, $role));
}

function is_logged_in()
{
  $this->session->flashdata('is_logged_in');
  $is_logged_in = $this->session->userdata('is_logged_in');
  
  if ( !isset($is_logged_in) || $is_logged_in != 1 )
   return FALSE;
  else
   return TRUE;
}
}
?>

What is wrong with my code?
#2

[eluser]InsiteFX[/eluser]
Try this:
Code:
function logout()
{
    $this->session->set_userdata(array('username' => '', 'is_logged_in' => 0));

    // or this.
    //$this->session->userdata = array();

    $this->session->sess_destroy();
    
    log_message('debug', 'Some variable was correctly set');

    redirect('login','refresh');
}
#3

[eluser]zqkun[/eluser]
[quote author="InsiteFX" date="1328093235"]Try this:
Code:
function logout()
{
    $this->session->set_userdata(array('username' => '', 'is_logged_in' => 0));

    // or this.
    //$this->session->userdata = array();

    $this->session->sess_destroy();
    
    log_message('debug', 'Some variable was correctly set');

    redirect('login','refresh');
}
[/quote]

Thanks for your reply. I tried with your suggested logout(), but it is still not able to logout after second time loginSad
#4

[eluser]InsiteFX[/eluser]
Maybe your browser is caching your pages.
#5

[eluser]zqkun[/eluser]
Yes, I am using firefox, but most browser are caching pages right? Does this means that the session function is not working with a caching enabled browser?
#6

[eluser]Unknown[/eluser]
i was have trouble like this...but i try to write code like this..and its work on codeigniter 2.10..
Code:
function logout()
{
    session_destroy();

    redirect('login','refresh');
}
#7

[eluser]InsiteFX[/eluser]
Try this
Code:
$user_session_data = array(
     'username'     => '',
     'is_logged_in' => 0
);

$this->session->set_userdata($user_session_data);
$this->session_destroy();

One thing to keep in mind is that session_destroy uses the session cookie for the session_id
to destroy the session.

So always clear out your userdata and then destroy the session.




Theme © iAndrew 2016 - Forum software by © MyBB