user2396285
user2396285

Reputation: 453

CodeIgniter, Using Redis as session manager

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?

Upvotes: 4

Views: 12327

Answers (3)

ABDUL AHAAD Shahid
ABDUL AHAAD Shahid

Reputation: 1

Enable Redis Session in CodeIgniter 3
step 1: install php_redis.dll or .so if using Windows/Linux according to: Note: check right architecture (x64/486) and PHP version check if thread safety enabled then download DLL file (architecture (x64/486)) Thread Safe else (architecture (x64/86)) non Thread
https://pecl.php.net/package/redis

For PHP version 7.1.4: Redis version 3.1.1
For PHP version 8.0.3: Redis version 5.3.6

Rest of process is same codeigniter 3 has defult session driver
session_redis_driver.php
step 2:
Then download php _redis.dll then copy file & paste into PHP /ext/ folder Then add in php.ini file
extension=php_redis.dll
session.save_path="tcp://127.0.0.1:6379"
session.save_handler = "redis"

Then check if Redis is enabled in phpinfo() page some thing like this version may vary
enter image description here

Step 3: add this setting in config.php file and comment file base session and add redis base sesson code
Follow this github repo for sample application https://github.com/Aadichohan/CodeIgniter-Redis-Session

//$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'] = 7200;
//$config['sess_regenerate_destroy'] = FALSE;

$config['sess_driver'] = 'redis';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
// $config['sess_save_path'] = 'tcp://localhost:6379';
$config['sess_save_path'] = 'tcp://localhost:6379?auth=123';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

Redis Commands:
on redis-cli
ping: to check if server is running result is pong
set
set name Ali
get
set name
result: Ali
flushall : to remove all cachedata
keys *: to get all keys
monitor: to monitor realtime cache addition

CONFIG SET requirepass passvalue: to set password
CONFIG SET requirepass "": to remove password

AUTH : to login into cli

Upvotes: 0

Cristiana Chavez
Cristiana Chavez

Reputation: 11519

Redis session is supported in Codeigniter version 3

Edit config file

$config['sess_driver'] = 'redis'; //enable redis session
$config['sess_save_path'] = 'tcp://localhost:6379'; // redis hostname:port 

Official Documentation Redis Driver

Upvotes: 1

xsist10
xsist10

Reputation: 3051

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.

Upvotes: 1

Related Questions