• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用Redis存储Session,如果只设置一个值就无法删除

php 搞代码 3年前 (2022-01-25) 23次浏览 已收录 0个评论
文章目录[隐藏]

<body>

如图所示,执行unset($_SESSION[‘name’])是无法删除的;
只有在有多个session,并且name不在首位才能被删除,在首位的session都不能被删除。请问这个情况大家遇到吗?

我在线上和线下都试过,无法解决


找到问题所在了,是由于我改写了session_set_save_handler()方法:

<code><?php/** * Session Redis存贮 * @version            v1.0 * @author             Lay */class Session_Redis extends Session {    /**     * @var string 键前缀     */    const CACHE_KEY = 'SESSION:';    /**     * @var object 数据库对象     */    private $_cache = null;    /**     * 开始Session     *     * @return void     */    public function __construct($cache) {        $this->_cache = $cache;        $this->_lifetime = ini_get('session.gc_maxlifetime');        session_set_save_handler(            array($this, '_open'),            array($this, '_close'),            array($this, '_read'),            array($this, '_write'),            array($this, '_destroy'),            array($this, '_gc')        );    }    // _open    public function _open($savePath, $name) {        return true;    }    // _close    public function _close() {        $this->_gc($this->_lifetime);        return true;    }    // _read    public function _read($id) {        $res = $this->_cache->get(self::CACHE_KEY . $id, FALSE);        return $res ? $res : '';    }    // _write    public function _write($id, $value) {        if ($value) {            $flag = $this->_cache->set(self::CACHE_KEY . $id, $value, intval($this->_lifetime), FALSE);            return $flag;        } else {            return FALSE;        }    }    // _destroy    public function _destroy($id) {        $flag = $this->_cache->delete(self::CACHE_KEY . $id);        return $flag;    }    // _gc    public function _gc($lifetime) {    }}</code>

调用:

<code>$cache = load_cache(); // 返回Redis对象$handle = new Session_Redis($cache);</code>

代码有什么问题吗?

回复内容:

<body>

如图所示,执行unset($_SESSION[‘name’])是无法删除的;
只有在有多个session,并且name不在首位才能被删除,在首位的session都不能被删除。请问这个情况大家遇到吗?

我在线上和线下都试过,无法解决


找到问题所在了,是由于我改写了session_set_save_handler()方法:

<code><?php/** * Session Redis存贮 * @version            v1.0 * @author             Lay */class Session_Redis extends Session {    /**     * @var string 键前缀     */    const CACHE_KEY = 'SESSION:';    /**     * @var object 数据库对象     */    private $_cache = null;    /**     * 开始Session     *     * @return void     */    public function __construct($cache) {        $this->_cache = $cache;        $this->_lifetime = ini_get('session.gc_maxlifetime');        session_set_save_handler(            array($this, '_open'),            array($this, '_close'),            array($this, '_read'),            array($this, '_write'),            array($this, '_destroy'),            array($this, '_gc')        );    }    // _open    public function _open($savePath, $name) {        return true;    }    // _close    public function _close() {        $this->_gc($this->_lifetime);        return true;    }    // _read    public function _read($id) {        $res = $this->_cache->get(self::CACHE_KEY . $id, FALSE);        return $res ? $res : '';    }    // _write    public function _write($id, $value) {        if ($value) {            $flag = $this->_cache->set(self::CACHE_KEY . $id, $value, intval($this->_lifetime), FALS<mark style="color:transparent">本%文来源gaodaimacom搞#^代%!码网@</mark>搞代gaodaima码E);            return $flag;        } else {            return FALSE;        }    }    // _destroy    public function _destroy($id) {        $flag = $this->_cache->delete(self::CACHE_KEY . $id);        return $flag;    }    // _gc    public function _gc($lifetime) {    }}</code>

调用:

<code>$cache = load_cache(); // 返回Redis对象$handle = new Session_Redis($cache);</code>

代码有什么问题吗?

没有遇到过,你确认下是不是被重新生成了,可以unset($_SESSION[‘name’])以后,给$_SESSION[‘name’]重新赋值试试。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用Redis存储Session,如果只设置一个值就无法删除

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址