Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter 3 and sessions

I updated to CodeIgniter 3 recently, following this guide: CI3: upgrade 3.0 from 2.2.1.

I set up this configuration in application/config/config.php file:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session_my_site';
$config['sess_expiration'] = 604800; // 1 week
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;

Is there something wrong here? My session is destroyed after a few hours...

like image 672
maxime1992 Avatar asked Feb 07 '15 18:02

maxime1992


People also ask

What is session CodeIgniter 3?

The Session class permits you maintain a user's “state” and track their activity while they browse your site. CodeIgniter comes with a few session storage drivers: files (default; file-system based) database.

How does session work in CodeIgniter?

If sessions data does not exist (or if it has expired) a new session will be created and saved in the cookie. If a session does exist, its information will be updated and the cookie will be updated. With each update, the session_id will be regenerated.

What is Flashdata CodeIgniter?

CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared. This can be very useful, especially for one-time informational, error or status messages (for example: “Record 2 deleted”).

How do you destroy a session in ci4?

To clear the current session (for example, during a logout), you may simply use either PHP's session_destroy() function, or the library's destroy() method.


1 Answers

In your save path you need to set up a location folder. Use 'files' as session driver preferred. As like below I have set up a cache to store sessions in BASE PATH which is setting folder. Make sure you have auto loaded sessions and $config['encryption_key'] = '' add key.

You can set up a database sessions but this works just as well. Make sure folder permissions 0700

http://www.codeigniter.com/userguide3/search.html?q=session&check_keywords=yes&area=default

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1440;
$config['sess_save_path'] = BASEPATH . 'yourfoldername/cache/';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;

Once that is done then you should be able to do something like.

$this->load->library('session');
$data_session_set = array('logged' => 1, 'username' => $this->input->post('username'), 'user_id' => $this->user_auth->get_id()); 
$this->session->set_userdata($data_session_set);
like image 169
Mr. ED Avatar answered Sep 26 '22 00:09

Mr. ED