1
votes

I have problem with sessions in CodeIgniter. When user log in to system I have this code:

$data_login = array(
    'id' => $user->id,
    'name' => $user->name,
    'email' => $user->email,
    'logged_in' => true
);
$this->session->set_userdata($data_login);

My sessions config:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

In general, everything works but when I do not log out and close the page tab, then I will visit the site again $this->session->userdata('logged_in') returns NULL. When I enter another subpage, everything returns to normal - recognizes the logged in user.

Where is problem? :/

@edit:

Here is my cookie config:

$config['cookie_prefix']    = '';
$config['cookie_domain']    = '';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

@edit2:

I noticed that this happened on the new hosting. Everything works on old hosting.

1
where's your session_start?treyBake
@treyBake no need to start session in Codeigniter.Devsi Odedra
@DevsiOdedra not used before^^ seems weird though, feel like you should init a session each time you use one (or use a lib function to init the session) - seems like it would be inefficient to handle session per session call ..treyBake
@treyBake there probably is a session_start being called when the framework is bootstrapped.Script47
@treyBake yes CI having their own lib to manage session and started session.Devsi Odedra

1 Answers

0
votes
   $data_login = array(
        'id' => $user->id,
        'name' => $user->name,
        'email' => $user->email
       );

   $this->session->set_userdata('logged_in', $data_login);

My sessions config :

   $config['sess_driver'] = 'files';
   $config['sess_cookie_name'] = 'ci_session';
   $config['sess_expiration'] = 7200;
   $config['sess_save_path'] = NULL;
   $config['sess_match_ip'] = FALSE;
   $config['sess_time_to_update'] = 300;
   $config['sess_regenerate_destroy'] = FALSE;

Don't forgot load session library

   $this->load->library('session');