I want use session in my website but I can't find best way to store them, codeigniter offers files, database and so on. but which is best??
I use file with this config:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_save_path'] = '_s';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 0;
$config['sess_regenerate_destroy'] = FALSE;
but i get this error:
Unable to create file ./_s\ci_sessionecc1dccdd1118e02ee956dde8aadaf7f1116c1ac because No such file or directory
I must use database ??
Add Session Data The same thing can be done in CodeIgniter as shown below. $this->session->set_userdata('some_name', 'some_value'); set_userdata() function takes two arguments. The first argument, some_name, is the name of the session variable, under which, some_value will be stored.
Once you have created your database table you can enable the database option in your config. php file as follows: $config['sess_use_database'] = TRUE; Once enabled, the Session class will store session data in the DB.
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”).
The sess_time_to_update setting is how often the session details (such as last activity) are updated. The default is every 5 minutes (300 seconds) and a new session ID will be generated.
I find the best way is to create a cache/session/ folder in your systems directory is more safer I put important things like logs and cache in system rather than application folder.
http://www.codeigniter.com/user_guide/libraries/sessions.html
Config.php
$config['encryption_key'] = 'somekey';
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = BASEPATH . 'cache/sessions/';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
I also would autoload sessions my self
application/config/autoload.php
$autoload['libraries'] = array('database', 'session');
Usage Example
On login form_validation success part
$data = array(
'is_logged' => true,
'username' => $this->input->post('username')
);
$this->session->set_userdata($data);
One you set your data then after login can get sessions $this->session->userdata('username')
etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With