CodeIgniter Tempdata
CodeIgniter also supports “temp data”, with a specific expiration time. After the value expires, or the session is deleted, the session value is automatically removed. To mark an existing item as “tempdata”, simply pass its key and expiry time (in seconds!) to the mark_as_temp() method:
Add Tempdata
we have to use mark_as_tempdata() function. This function takes two argument items or items to be stored as tempdata and the expiration time for those items are as shown code below.
// 'item' will be erased after 360 seconds (6 minutes)
$this->session->mark_as_temp('item', 360);
You can also pass an array to store multiple data. All the items stored below will be expired after 360 seconds.
// 'item' will be erased after 360 seconds (6 minutes)
$this->session->mark_as_temp(array('item','item1'), 360);
You can also set different code expiration time for each item as shown below.
// 'item' will be erased after 360 seconds, while 'item2'
// will do so after only 240 seconds
$this->session->mark_as_temp(array(
'item'=>360,
'item2'=>240
));
Or alternatively, using the set_tempdata() method:
$this->session->set_tempdata('item', 'value', 360);
Remove CodeIgniter Tempdata
CodeIgniter Tempdata session is evacuated naturally after its lapse time yet in the event that you need to expel CodeIgniter Tempdata session before that, then you can do as appeared beneath utilizing the unset_tempdata() function work, which takes one contention of the outcome to be expelled.
$this->session->unset_tempdata('item');
CodeIgniter Session Preferences
CodeIgniter will usually make everything work out of the box. However, Sessions data are a very sensitive component of any application, so some careful configuration must be done. Please take your time to consider all of the options and their effects.
If you want to change session driver and storage path or any more session settings. you'll find the following Session related preferences in your application/config/config.php file:
Default session setting
$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;
Database Driver
The ‘database’ driver uses a relational database such as MySQL or PostgreSQL to store sessions
if you would like to use ‘tbl_sessions’ as your table name, you would do this:
$config['sess_driver'] = 'database';
$config['sess_save_path'] = 'tbl_sessions';
Database Table For MySQL :
CREATE TABLE IF NOT EXISTS `tbl_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);
Database Table For PostgreSQL:
CREATE TABLE "tbl_sessions" (
"id" varchar(128) NOT NULL,
"ip_address" varchar(45) NOT NULL,
"timestamp" bigint DEFAULT 0 NOT NULL,
"data" text DEFAULT '' NOT NULL
);
CREATE INDEX "ci_sessions_timestamp" ON "tbl_sessions" ("timestamp");
You will also need to add a PRIMARY KEY depending on your ‘sess_match_ip’ setting. The examples below work both on MySQL and PostgreSQL:
// When sess_match_ip = TRUE
ALTER TABLE ci_sessions ADD PRIMARY KEY (id, ip_address);
// When sess_match_ip = FALSE
ALTER TABLE ci_sessions ADD PRIMARY KEY (id);
// To drop a previously created primary key (use when changing the setting)
ALTER TABLE ci_sessions DROP PRIMARY KEY;