session redis驱动

浏览:1944 发布日期:2016/03/16 分类:求助交流
tp session 用redis 保存数据的是怎么是空的?键为空
打印了$sessData 是空的,求教怎么办,急

public function write($sessID, $sessData){}

以下驱动源码
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace Think\Session\Driver;

use Think\Exception;

class Redis
{
protected $handler = null;
protected $config = [
'host' => '127.0.0.1', // redis主机
'port' => 6379, // redis端口
'password' => '', // 密码
'expire' => 3600, // 有效期(秒)
'timeout' => 0, // 超时时间(秒)
'persistent' => true, // 是否长连接
'session_name' => '', // sessionkey前缀
];

public function __construct($config = [])
{
$this->config = array_merge($this->config, $config);
// 检测php环境
if (!extension_loaded('redis')) {
throw new Exception('_NOT_SUPPERT_:redis');
}
$this->handler = new \Redis;
// 建立连接
$func = $this->config['persistent'] ? 'pconnect' : 'connect';
$this->config['timeout'] > 0 ?
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']) :
$this->handler->$func($this->config['host'], $this->config['port']);
if ('' != $this->config['password']) {
$this->handler->auth($this->config['password']);
}
}

/**
* 打开Session
* @access public
* @param string $savePath
* @param mixed $sessName
*/
public function open($savePath, $sessName)
{
return true;
}

/**
* 关闭Session
* @access public
*/
public function close()
{
$this->gc(ini_get('session.gc_maxlifetime'));
$this->handler->close();
$this->handler = null;
return true;
}

/**
* 读取Session
* @access public
* @param string $sessID
*/
public function read($sessID)
{
return $this->handler->get($this->config['session_name'] . $sessID);
}

/**
* 写入Session
* @access public
* @param string $sessID
* @param String $sessData
*/
public function write($sessID, $sessData)
{
if (!$sessData) {
return true;
}
if ($this->config['expire'] > 0) {
return $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData);
} else {
return $this->handler->set($this->config['session_name'] . $sessID, $sessData);
}
}

/**
* 删除Session
* @access public
* @param string $sessID
*/
public function destroy($sessID)
{
return $this->handler->del($this->config['session_name'] . $sessID);
}

/**
* Session 垃圾回收
* @access public
* @param string $sessMaxLifeTime
*/
public function gc($sessMaxLifeTime)
{
return true;
}

public function __destruct() {
if ($this->config['persistent'] == 'pconnect') {
$this->handler->close();
}
session_write_close();
}
}

附件 微信截图_20160316174427.png ( 22.97 KB 下载:31 次 )

最佳答案
评论( 相关
后面还有条评论,点击查看>>