What I want to implement is a simple login page, if user login successfully, then redirect to main page, else remain login page.
I have 1 controller named login, and 1 model named main.
When user click the login button, the login/login_send will be called.
<?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
?>
Model_login here is just to help to verify if user is logged in, by check the session data.
<?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');
    return ($this->_isUserExist($username, $password));
}
function 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;
}
}// end class model_login
?>
When first time login, and then logout, there is no problem. However, if I login second time, I can not log out. Even the login/logout was called correctly, I also refreshed the page, but the session['is_logged_in'] == 1. Something wrong with my code?
In your application/config/config.php try changing
$config['sess_time_to_update']  = 300; //This is the default setting in ver 2.1.1
to
$config['sess_time_to_update']  = 0;
This gave me some problems a few years back
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With