使用Redis存储Session,如果只设置一个值就无法删除
如图所示,执行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>
代码有什么问题吗?
回复内容:
如图所示,执行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>
代码有什么问题吗?
没有遇到过,你确认下是不是被重新生成了,可以unset($_SESSION['name'])以后,给$_SESSION['name']重新赋值试试。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。
