0
votes

I'm new to codeigniter and so when I set up my log in code I started out with simple and kept updating it to be more complex/secure. With that said I was making great progress creating a session and adding a user_data variable called "login_status" set to "1". To use as a reference for future page requests. Eventually I decided to go ahead and set up the database table ci_sessions and switch to that instead of just using a cookie. When I did this, all of the sudden my "login_status" variable was not being written anymore. As a result I could no longer access any subsequent pages and kept being redirected back to the log in screen.

In short, this exact same code works perfectly when I have sess_use_database set to false.

I'm not sure why this is happening but any help would be greatly appreciated!

Log in Controller:

class login extends CI_Controller 
{

function __construct() {
    parent::__construct();
    $this->load->library('session');
    $this->load->helper('url');
    $this->load->helper('form');
}

public function index($login = "") 
{
    $data = array();
    if ($login == "failed")
        $data['loginFailed'] = true;
    else
        $data['loginFailed'] = false;

    $this->load->view('templates/headerAdmin');
    $this->load->view('admin/loginform', $data);
    $this->load->view('templates/footerAdmin');
}

public function assessme()
{
    $username = $_POST['username'];
    $password = md5($_POST['password']);


    //checkme works fine and returns true
    if ($this->checkme($username, $password))
    {
        $newdata = array( 'login_status' => '1' );
        $this->session->set_userdata($newdata);
        $this->mainpage();
    }
    else {$this->index("failed");}
}

public function checkme($username = "", $password = "")
{
    if ($username != "" && $password != "") 
    {
        $this->load->model('admin/loginmodel');
        if ($this->loginmodel->validateCredentials($username, $password))
            return true;
        else   
            return false;
    }
    else 
    {
        return false;
    }
}

public function mainpage()
{
    redirect('admin/dashboard');
}

The controller that I am redirected to after I can successfully log in:

class dashboard extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->library('session');
    $this->load->helper('url');
}

public function index() {

    //Make sure user is logged in
    $login_status = $this->session->userdata('login_status');

    //This is where I am redirected because the user_data is not being set
    if(!isset($login_status) || $login_status != '1') {
        redirect('admin/login');
    }

    $this->load->view('templates/headerAdmin');
    $this->load->view('admin/dashboard/list');
    $this->load->view('templates/footerAdmin');

}

}

Config:

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

EDIT : Setting 'sess_match_useragent' to FALSE seems to prevent the session from being destroyed. Hopefully that will provide other clues as to what the cause of my problem is but obviously this, in itself, isn't an ideal solution

1
Just checking the obvious... you've created the database table and there is a user_data field, right? And have you got database in the libraries array of your autoload.php file? (Thinking about it, you may as well put session in your autoload libraries too. - Mat
Yes I do. It is writing everything else involved with the session to the database just fine. - Reasoned
@Reasoned what the echo $this->session->all_userdata(); is returning? have you tryed putting $this->output->enable_profiler(TRUE); in your script? :) - itsme
Same thing, with sess_use_database set to FALSE it displays login_status set to 1. With sess_use_database set to TRUE no userdata is displayed - Reasoned

1 Answers

-1
votes

I had this problem and I fix it by this page

http://philsbury.co.uk/blog/code-igniter-sessions

This page say u should change Session library in \system\libraries I Rename default Session file And create new Session file and put the code on it

its Worked