aaa
aaa

Reputation: 73

CodeIgniter store PHP session in Redis

I'm following an article to save PHP sessions in Redis rather than the filesystem (so I can share session with node.js)

I understand how session_set_save_handler works but i'm confused on how to set it up with the CodeIgniter framework? Should I extend the CI_Session class and put the session_set_save_handler in there?

Upvotes: 2

Views: 3660

Answers (2)

Cristiana Chavez
Cristiana Chavez

Reputation: 11519

Redis session is supported in Codeigniter version 3

Open and Edit

application/config/config.php

To enable Redis set session driver to "redis"

$config['sess_driver'] = 'redis';

Example of redis server path

redis://hostname:port or tcp://hostname:port

Set the value of sess_save_path localhost for local testing

$config['sess_save_path'] = 'tcp://localhost:6379';

To test Redis on your localhost.

Please refer to my answer on how to install Redis for Windows

Upvotes: 1

Umair Ahmed
Umair Ahmed

Reputation: 26

Store your session in database. In order to store sessions, you must first create a database table for this purpose. Here is the basic prototype (for MySQL) required by the session class:

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(45) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

SET

$config['sess_use_database'] = TRUE;

in config.php FILE.

Once enabled, the Session class will store session data in the DB.

Make sure you've specified the table name in your config file as well:

$config['sess_table_name'] = 'ci_sessions';

Upvotes: 0

Related Questions