Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to store session in codeigniter

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 ??

like image 697
Mahdi Majidzadeh Avatar asked Jun 26 '15 04:06

Mahdi Majidzadeh


People also ask

How can store session in CodeIgniter?

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.

How can store session ID in database in CodeIgniter?

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.

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”).

What is Sess_time_to_update?

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.


1 Answers

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

like image 106
Mr. ED Avatar answered Sep 21 '22 20:09

Mr. ED