2

When I enter my credentials for my login program developed in Codeigniter the sessions don't seem to be saving in the ci_sessions table in the MySQL database.

Once the form is filled in, the session will be set in the controller.php (sample code below):

if ($this->form_validation->run()) {
    $data = array(
       'email' => $this->input->post('email'), // posting the email input field
       'is_logged_in' => 1 
    );

$this->session->set_userdata($data);  

redirect('main/members');

} else {

    $this->load->view('login');

}  

At the moment its outputting the following when executing:

Array
(
   [__ci_last_regenerate] => 1440877455
   [email] => [email protected]
   [is_logged_in] => 1
)

The output is using the following code in view.php

print_r($this->session->all_userdata());

The array should be outputting something similar below but its not:

Array
(
   [session_id] => dd7e0e2266da6481ef45faaa7e3c808b
   [ip_address] => 127.0.0.1
   [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
   [last_activity] => 1387619782
   [user_data] => 
   [username] => Jhon
   [email] => [email protected]
)

config.php:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

autoload.php

$autoload['libraries'] = array('database', 'session', 'form_validation');

$autoload['drivers'] = array('session');

$autoload['helper'] = array('url', 'form', 'html', 'security');

I have checked the config.php to ensure everything is set correctly and referred to the manual on Codeigniters website but still no joy in solving the issue.

Any idea's as to why this is happening? OR Any possible solutions?

10
  • Output points you have is_logged and is_logged_in properties. Where is set first one? Commented Aug 29, 2015 at 22:35
  • @Tpoika - I am currently using 'is_logged_in' which is set in the array. Commented Aug 31, 2015 at 9:23
  • Have you tried debugging proposal from answer bellow? Commented Aug 31, 2015 at 9:29
  • @Tpoika - I have I get the following: 'array(2) { ["email"]=> string(24) "[email protected]" ["is_logged_in"]=> int(1) }' It seems its not saving to the database. I have checked the autoload.php, config.php and database.php and all seems to be configured correctly. Commented Aug 31, 2015 at 10:13
  • If you use database method for sessions, it is already in db than. You just print it out. Commented Aug 31, 2015 at 10:16

2 Answers 2

3

There's a number of problems here ...

At the moment its outputting the following when executing:

Array
(
  [__ci_last_regenerate] => 1440877455
  [email] => [email protected]
  [is_logged_in] => 1
)

...

The array should be outputting something similar below but its not:

Array
(
  [session_id] => dd7e0e2266da6481ef45faaa7e3c808b
  [ip_address] => 127.0.0.1
  [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
  [last_activity] => 1387619782
  [user_data] => 
  [username] => Jhon
  [email] => [email protected]
)

No it shouldn't ... you're not setting "username" anywhere and the rest is explained in the manual.


Let's inspect your configuration now ...

$config['sess_driver'] = 'files';

^ This line is what tells the library what storage to use. If it is set to 'files', you cannot expect sessions to be saved to the database ...

$config['sess_encrypt_cookie'] = FALSE;

^ This setting is obsolete; it does nothing on CI 3.0.x.

$config['sess_use_database'] = TRUE;

^ Since you have set $config['sess_driver'], this setting is ignored - remove it.

$config['sess_table_name'] = 'ci_sessions';

^ This is also ignored, and you shouldn't be using it with CI3. Set $config['sess_save_path'] instead.

$config['sess_save_path'] = NULL;

^ You're not supposed to leave this to NULL. Both the manual and the inline comments in the stock config.php file say that setting this is REQUIRED!

$config['sess_match_useragent'] = FALSE;

^ This also does nothing in CI3.

$autoload['drivers'] = array('session');

^ Nothing in the documentation suggests this and it does nothing.


I have checked the config.php to ensure everything is set correctly and referred to the manual on Codeigniters website but still no joy in solving the issue.

On the contrary, you didn't pay attention even to the most basic documentation, which are short descriptions in the config files ... You've obviously just copy-pasted your old settings from CodeIgniter 2.x and didn't follow the upgrade instructions.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is what i will advice you to do

$data = array(
       'email' => $this->input->post('email'), // posting the email input field
       'is_logged_in' => 1 
    );
//Use Var_dump($data) directly under the code above and add a die statement to check if the array actually contain the data as specified above
$this->session->set_userdata($data); 

/*
** store the session inside a variable then var_dump it if it is really 
* in there
*/

$result = $this->session->set_userdata($data);
var_dump($result);die;

Note the changes and try to replicate samething just to confirm if those data are actually stored in an array and also to check if the session is available inside the $result

3 Comments

When I debugged the array using the code above I get the following: array(2) { ["email"]=> string(24) "[email protected]" ["is_logged_in"]=> int(1) }, at the moment it doesn't seem to be saving the session in the database.
Did you configure your session to be saved in the Database?
Yes I have see config.php in my post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.