CodeIgniter, Using Redis as session manager

user2396285 picture user2396285 · Jun 10, 2013 · Viewed 7.4k times · Source

From github the configuration claims that I need to set

$config['sess_use_database'] = TRUE;

This is required to store the session data in redis.

However, from CodeIgniter's guide If $config['sess_use_database'] = TRUE; session class will store session data in the DB.

I want to understand, how the database write is bypassed even if we set 'sess_use_database' config to true.

Secondly, the method requires phpredis extension to be installed.

I already have another CI redis library installed from the following github repository.

Can someone help me to configure the code from ericterpstra, [File Name: ci_sock/part_two/MY_Session.php] so that it can use the above library, instead of phpredis?

Answer

Tom picture Tom · Feb 3, 2015

If I'm not mistaken, phpredis is a C extension for PHP, where the CI redis library is a pure PHP implementation of a redis client. It is probably possible for you to have phpredis installed as an extension and still use your CI redis library (although you might want to dump it as it is no longer maintained).

If you do install phpredis you can bypass the whole CodeIgnitor session configuration issue by just switching the default session handler in your php.ini file to use redis.

Here is an example from https://github.com/phpredis/phpredis#php-session-handler:

session.save_handler = redis
session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2"

This will transparently use your redis storage to store sessions.