using redis for php sessions

558 views
Skip to first unread message

w-ll

unread,
Apr 22, 2010, 4:36:57 PM4/22/10
to Redis DB
long story short i got redis and phpredis mod working.
simple set/gets work.

when i tried to implement a session handler it does write. any
thoughts


error_reporting(E_ALL);
ini_set('dispaly_errors',1);

function open($save_path, $session_name){
global $redis;

$redis = new Redis();
$redis->connect('127.0.0.1',6379);

return(true);
}

function close(){
return(true);
}

function read($id){
global $redis;
return $redis->get($id);
}

function write($id, $sess_data){
global $redis;
print_r( $redis->set($id,'x'));
}


function destroy($id){
global $redis;
return $redis->delete($id);
}

function gc($maxlifetime){

return true;
}

session_set_save_handler("open", "close", "read", "write", "destroy",
"gc");

session_start();

echo $_SESSION['foo'].'<br>';
echo $_SESSION['bar'].'<br>';

$_SESSION['foo'] = 'baz';
$_SESSION['bar'] = 'bugz';

echo $_SESSION['foo'].'<br>';
echo $_SESSION['bar'].'<br>';

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.

Pablo Viojo

unread,
Apr 22, 2010, 6:36:52 PM4/22/10
to redi...@googlegroups.com
I've implemented a redis session handler with a similar code and it works perfectly. Can you elaborate a little more on your problem?

dubayou

unread,
Apr 22, 2010, 6:50:14 PM4/22/10
to redi...@googlegroups.com
oops in the code as i sent it, on the session_write it is set to $key
and then just 'x' now but i had the $data var.

the problem is that its not saving. can I see your code?

will

Pablo Viojo

unread,
Apr 22, 2010, 11:29:20 PM4/22/10
to redi...@googlegroups.com
It's almost exactly like yours. This is a simplified version (mine use a connection factory an some other optimizations)

<?
function sr_open($path, $name){
//Do nothing now
return true;
}

function sr_close(){
//Do nothing now
return true;
}

function sr_read($id){
$redis = getRedisConn();
$id = "_sess_" . $id;
        return $redis->get($id);
}

function sr_write($id, $sess_data){
$redis = getRedisConn();
$id = "_sess_" . $id;
return $redis->set($id, $sess_data);
}


function sr_destroy($id){
$redis = getRedisConn();
$id = "_sess_" . $id;
return $redis->delete($id);
}

function sr_gc($lifetime){
//Do nothing now
return true;
}

function getRedisConn(){
$redis = new Redis();
        $redis->connect('127.0.0.1',6379);
return $redis;
}

session_set_save_handler("sr_open", "sr_close", "sr_read", "sr_write", "sr_destroy","sr_gc");




Regards!
Reply all
Reply to author
Forward
0 new messages