Codeigniter - Session data not saving in database

2019-05-29 15:40发布

问题:

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] => jhon@gmail.com
   [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] => jhon@gmail.com
)

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?

回答1:

There's a number of problems here ...

At the moment its outputting the following when executing:

Array
(
  [__ci_last_regenerate] => 1440877455
  [email] => jhon@gmail.com
  [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] => jhon@gmail.com
)

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.



回答2:

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