After reading the official documentation
In config.php
You need to modify
FROM
$config['sess_driver'] = 'files';
$config['sess_save_path'] = NULL;
TO
$config['sess_driver'] = 'database';
$config['sess_save_path'] = 'ci_sessions';
as well as you need to create the table by the name ci_sessions
in case if it doesn't exist.
In order to use the ‘database’ session driver, you must also create this table that we already mentioned and then set it as your $config['sess_save_path']
value. For example, if you would like to use ‘ci_sessions’ as your table name, you would do this:
Note
If you’ve upgraded from a previous version of CodeIgniter and you don’t have ‘sess_save_path’ configured, then the Session library will look for the old ‘sess_table_name’ setting and use it instead. Please don’t rely on this behavior as it will get removed in the future.
And then, of course, create the database table …
For MySQL:
CREATE TABLE IF NOT EXISTS `ci_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`)
);
For PostgreSQL:
CREATE TABLE "ci_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 "ci_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;