Welcome Guest, Not a member yet? Register   Sign In
Code Igniter keeps losing my session ID
#1

I've posted this question on Stack Overflow - I'm going to cover my bases and post it here as well.

In my attempts to store session information in Code Igniter I am running into a pretty frustrating problem. Disclaimer - I am still quite green with both PHP and CI so please be kind...

Another source of assistance has given me This code to test and it works fine with the following configuration -

Code:
   $config['sess_driver'] = 'database';
   $config['sess_cookie_name'] = 'QATime';
   $config['sess_expiration'] = 7200;
   $config['sess_save_path'] = 'ci_sessions';
   $config['sess_match_ip'] = FALSE;
   $config['sess_time_to_update'] = 300;
   $config['sess_regenerate_destroy'] = FALSE;
   .
   .
   .
   $config['cookie_prefix']    = '';
   $config['cookie_domain']    = '';
   $config['cookie_path']        = '/';
   $config['cookie_secure']    = FALSE;
   $config['cookie_httponly']     = FALSE;

Please note - this works fine. The database stores the values in the simple test and they are loaded again when the "web page" is reloaded. When I say that this 'works', what I mean is that when I run it, I get the following -

[Image: Qe5Qv.png]

When I terminate and relaunch I get the following -

[Image: ex1Vm.png]

To me this says that the session data and database information is working as it should be, and there was much rejoicing.

What does go wrong is when I attempt to log in. The session information is... 'lost' or something somehow and I can't figure out why that is happening.

This is the chain of execution as best as I can trace it -

Within the `routes.php` I have the following -

Code:
$route['default_controller'] = 'Main/view';

Within the `Main` class `view` method I have the following -

Code:
$this->load->model('User_Model', 'User');

The User_Model class definition and constructor -

Code:
CLASS User_Model EXTENDS MY_Model{
    public FUNCTION __construct(){
        PARENT::__construct();
        $this->database = $this->load->database('users', TRUE);
        $this->table = 'users';
        $this->idKey = 'UserID';
    }
    .
    .
        .
}

MY_Model definition and constructor -

Code:
CLASS MY_Model EXTENDS CI_Model{
    
    public $ID;
    protected $database, $table, $idKey, $row;
    .
    .
    .
    public FUNCTION __construct(){
        PARENT::__construct();
    }
    .
    .
    .
}

The next few lines within the `Main` class `view` method -

Code:
$this->load->library('Form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]|max_length[20]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[20]');
        
IF ($this->form_validation->run()){
    IF (!$this->User->login(
        addslashes(strtolower($this->input->post('username', TRUE))),
        addslashes($this->input->post('password', TRUE)),
            $this->getIP())){
                /*This block is executed fine and is not relevant to this question.*/
            } ELSE {
                /*We will come back to this after expounding the Main getIP() and User login(...) functions*/
            }

Main getIP Function -

Code:
private function getIP(){
    RETURN !EMPTY($_SERVER['HTTP_CLIENT_IP'])
        ? $_SERVER['HTTP_CLIENT_IP']            
        : !EMPTY($_SERVER['HTTP_CLIENT_IP'])
            ? $_SERVER['HTTP_CLIENT_IP']
            : $_SERVER['REMOTE_ADDR'];
}

User_Model login function -

Code:
public FUNCTION login($userName, $password, $IP){
    $row = $this->database->get_where('users', ARRAY('UserName' => $userName))->row_array()['UserName'];
    IF (!ISSET($row)){
        /*The row is set fine - this isn't relevant to the question.*/
    } ELSE {
        $row = $this->database->get_where('users', ARRAY('UserName' => $userName, 'Password' => $password)
            )->first_row('array');
        IF (!ISSET($row)){
            /*Not relevant to the question.*/
        } ELSEIF(FALSE /*Boolean function call not relevant to the question.*/){
            /*Not relevant to the question.*/
        } ELSE {
            $this->ID = $row['UserID'];
            $this->database->insert(
                'user_history',
                ARRAY('UserID' => $this->ID, 'LoginDate' => date('Y-m-d H:i:s'), 'IP' => $IP));
            /*$this->session->set_userdata($row)
            This I tried in the past but the Model evidently does not have
            implicit access to session data. I had attempted to circumvent
            this in the MY_Model implementation but decided that it would be
            better to handle this in the controller.*/
            RETURN TRUE;
        }
    }
}

Now we return to the `Main` `view` method execution where we left off -

Code:
} ELSE {
    $this->session->set_userdata($this->User->staleRow());
    SWITCH($this->User->stale('Permissions')){
        CASE 'ADMIN':
            redirect('Admin');
            BREAK;
        /*Non question relevant code omitted*/
    }
}

The User staleRow() function is actually inherited from the `MY_Model` class -

Code:
/**
* Returns an instantiated row of stale data.
*/
public FUNCTION staleRow(){
    if (!ISSET($this->row))
        $this->row = $this->freshRow();
    return $this->row;
}

/**
* Returns a row of fresh data.
*/
public FUNCTION freshRow(){
    return $this->database->
        select()->
        from($this->table)->
        where("$this->idKey = $this->ID")->
        get()->first_row('array');
}

And then we have the Admin class definition and constructor -

Code:
CLASS Admin EXTENDS CI_Controller{
    
    public FUNCTION __construct(){
        PARENT::__construct();
        $this->output->enable_profiler(TRUE);
        $this->load->model('User_Model', 'User');
        $this->User->ID = $this->session->UserID;
        IF (!$this->session->UserID){
            $this->session->set_flashdata('user_msg', 'Login Required');
            redirect(base_url(), 'refresh');
            EXIT;
        } ELSEIF ($this->session->Permissions != 'ADMIN'){
            $this->session->set_flashdata('user_msg', 'Admin Login Required');
            redirect(base_url(), 'refresh');
            EXIT;
        }
    }

The Admin class is wherein the problem lies - for reasons beyond what my limited experience permits me to comprehend, when it reaches this point, the session data is not present - Please see the following pictoral evidence -

[Image: 6A9Hu.png]

This is the session variable within the `Main` class after the line `$this->session->set_userdata($this->User->staleRow());` is called.

This is the session variable within the `Admin` class after the `PARENT::__construct();` method is called -

[Image: OCw0m.png]

I'm sure it's something I'm not doing correctly. Why is the session data not being loaded into the Admin class controller?


EDIT 1
------

Please know that I am running Code Igniter version 3.0.1

Below it was suggested I check the Cookie ID in a couple of places - this was the result:

After the 'login' button was clicked -
[Image: CNVTO.png]

From this I can see the Cookie ID within the array (QATime = some obscenely long integer value))

After the `redirect('Admin');` call, this value is no longer present -

[Image: kYhmR.png]

I understand the function of this ID is to help find session data, so it makes perfect sense to me that the session data is lost because this ID is not being kept. So why is the Cookie ID being lost?
Reply


Messages In This Thread
Code Igniter keeps losing my session ID - by Geoclasm - 09-15-2015, 10:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB