[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(
).
if user press logout, it will destroy session and redirect to login page(
).
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?