位置:首页  >   程序积累  > php使用redis替换文件存储session(session_set_save_handler)

php使用redis替换文件存储session(session_set_save_handler)

<?php
class MySessionHandler implements SessionHandlerInterface
{
    private $redis;
    private $sessionsavepath;
    private $sessionname;
    public function __construct()
    {
        $this->redis = new Redis();
        $this->redis->connect('127.0.0.1',6379);
    }
    public function close()
    {
        return true;
    }
    public function destroy($session_id)
    {
        if($this->redis->delete($session_id)){
            return true;
        }
        return false;
    }
    public function gc($maxlifetime)
    {
        return true;
    }
    public function open($sessionsavepath, $name)
    {
        return true;
    }
    public function read($session_id)
    {
        if($value = $this->redis->get($session_id)){
            return $value;
        }else{
            return false;
        }
    }
    public function write($session_id, $session_data)
    {
        if($this->redis->set($session_id,$session_data,60)){
            return true;
        }else{
            return false;
        }
    }

}

$handler = new MySessionHandler();
session_set_save_handler($handler, true);
session_start();

//$_SESSION['sess_name'] = 'xd';
var_dump($_SESSION);

文章属性
精彩评论