热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

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

使用Redis存储Session,如果只设置一个值就无法删除:如图所示,执行unset($_SESSION[name])是无法删除的;只有在有多个session,并且name不在首

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

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


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

_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) { } }

调用:

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

代码有什么问题吗?

回复内容:

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

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


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

_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) { } }

调用:

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

代码有什么问题吗?

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

推荐阅读
author-avatar
D调肥仔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有