使用Redis儲存Session,如果只設定一個值就無法刪除

來源:互聯網
上載者:User

,執行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']重新賦值試試。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.