We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

use Phalcon\Session\Adapter\Redis to store seesion's user

anyone can help me to store session on redis!!! can step by step....i use MVC same phalcon homepage.. and show tutorials thanks so much!

In your services.php:

$di->setShared('session',function(){
    $session = new \Phalcon\Session\Adapter\Redis(array(
    'uniqueId' => 'my-private-app',
      'host' => 'localhost',
      'port' => 6379,
      'auth' => 'foobared',
    'persistent' => false,
    'lifetime' => 3600,
    'prefix' => 'my_'
 ));
 return $session;
});

Basically that's all you have to do because Phalcon abstracts the session adapter.

More info in the docs: https://api.phalcon.io/class/Phalcon/Session/Adapter/Redis.html



85.5k

i use this btw

https://github.com/phpredis/phpredis

and its kind a "automatic", and its in C again same as the framework



4.3k

i config it in index.php file, same below

$di->set('session', function() { $session = new Redis([ 'path' => 'tcp://127.0.0.1:6379?weight=1' ]);

    $session->start();

    return $session;
});

what is the differents??? thanks bro

In your services.php:

$di->setShared('session',function(){
  $session = new \Phalcon\Session\Adapter\Redis(array(
   'uniqueId' => 'my-private-app',
    'host' => 'localhost',
    'port' => 6379,
    'auth' => 'foobared',
   'persistent' => false,
   'lifetime' => 3600,
   'prefix' => 'my_'
));
return $session;
});

Basically that's all you have to do because Phalcon abstracts the session adapter.

More info in the docs: https://api.phalcon.io/class/Phalcon/Session/Adapter/Redis.html



4.3k

so you can show me how config in project phalcon?? thanks so

i use this btw

https://github.com/phpredis/phpredis

and its kind a "automatic", and its in C again same as the framework

edited Jul '16

It looks like you are not using phalcon bulit-in class. I don't know is this option but it doesn't exists in phalcon _construct method - https://github.com/phalcon/cphalcon/blob/master/phalcon/session/adapter/redis.zep#L61

i config it in index.php file, same below

$di->set('session', function() { $session = new Redis([ 'path' => 'tcp://127.0.0.1:6379?weight=1' ]);

   $session->start();

   return $session;

}); what is the differents??? thanks bro

In your services.php:

$di->setShared('session',function(){
 $session = new \Phalcon\Session\Adapter\Redis(array(
   'uniqueId' => 'my-private-app',
   'host' => 'localhost',
   'port' => 6379,
   'auth' => 'foobared',
   'persistent' => false,
   'lifetime' => 3600,
   'prefix' => 'my_'
));
return $session;
});

Basically that's all you have to do because Phalcon abstracts the session adapter.

More info in the docs: https://api.phalcon.io/class/Phalcon/Session/Adapter/Redis.html



85.5k

that's the thing, you dont need to configure anything special. Just register session service and thats it.

so you can show me how config in project phalcon?? thanks so

i use this btw

https://github.com/phpredis/phpredis

and its kind a "automatic", and its in C again same as the framework

edited Nov '16

Sorry for asking my question here: I want to know that as you mentioned

$di->setShared('session',function(){

$session = new \Phalcon\Session\Adapter\Redis(array(    
'uniqueId' => 'my-private-app', 
  'host' => 'localhost',
  'port' => 6379,
  'auth' => 'foobared',
'persistent' => false,
'lifetime' => 3600,
'prefix' => 'my_'
));
 return $session;
});

similarly i want like:

$di->setShared('session',function(){
$session = new \Phalcon\Session\Adapter\files();
return $session;
});

And

$di->setShared('redisSession',function(){
$session = new \Phalcon\Session\Adapter\Redis(array(
'uniqueId' => 'my-private-app',
  'host' => 'localhost',
  'port' => 6379,
  'auth' => 'foobared',
'persistent' => false,
'lifetime' => 3600,
'prefix' => 'my_'
));
return $session;
});

So that i can use both, but Redis session is not working when i am doing like above. it is always storing in files not in Redis, even if i am calling $this->redisSession. When i remove session with file part($di->setShared('session')) it start storing in Redis.

i want a solution that when call $this->redisSession it should setup with Redis, and when $this->session it should save in files.

But why ? You can use redis or files. Not both. Why you need such a feature ?

Presently we are having Redis server on a separate machine, So I want to write both service. So any developer from team want access regular file sessions then he can, and specifically any one want Redis then he can also for that he need not change services.

As you say it not possible then is it possible that i can create a configuration setting which can decide that what will be your adaptor, So that if there is a down time of redis server or any xyz reason so we can switch back to files