2011-09-30 19 views
3

我在我正在構建的站點上使用最新版本的Codeigniter和tank_auth 1.0.9。使用坦克認證時,閃存數據不會存儲在重定向之間

當分別使用set_flashdata()和flashdata()時,在重定向時不會返回任何內容,但是如果我在配置中將sess_use_database設置爲FALSE,它將起作用。

我搜索了四周,找不到答案 - 有沒有其他人遇到這個問題,並修復它?

+1

您確定這裏和Tank_Auth有關嗎?嘗試在乾淨的安裝? –

+0

我建議將log_message(..)語句和檢查瀏覽器生成的每個http請求的flashdata的值。 codeigniter內部的重定向(使用redirect())也會導致flashdata被清除。檢查您的日誌文件是否有可能是錯誤的重定向() – Shivaas

回答

0

如果tank_auth執行任何內部重定向,那麼您可能會丟失該重定向請求上的Flash數據。

2

我有同樣的問題,並找出問題。如果您將會話存儲在數據庫中,則不起作用。

罐驗證運行從主庫這個代碼($this->tank_auth->logout()):

$this->delete_autologin(); 

// See http://codeigniter.com/forums/viewreply/662369/ as the reason for the next line 
$this->ci->session->set_userdata(array('user_id' => '', 'username' => '', 'status' => '')); 

$this->ci->session->sess_destroy(); 

然後,它運行在與身份驗證控制器這個代碼($this->_show_message()):

$this->session->set_flashdata('message', $message); 
redirect('/auth/'); 

的問題是,由於sess_destroy()在設置flashdata之前運行,沒有數據庫行來添加flashdata,所以flashdata永遠不會被設置。

在這一點上有幾個解決方案:

選項1:

$this->ci->session->sess_destroy();後,立即在功能logout()application/libraries/Tank_auth.php

這工作,因爲你正在創建一個新的空白會話添加$this->ci->session->sess_create(); flashdata可以存儲在哪裏。一個潛在的對策是你正在對數據庫執行更多的操作(刪除+插入)。

選項2:

註釋掉/刪除$this->ci->session->sess_destroy();在功能logout()application/libraries/Tank_auth.php

這工作,因爲該會議不被破壞,使CI只執行一次更新查詢添加flashdata。這可能比選項1更好,除非你絕對需要銷燬會話。

方案3:

設置$config['sess_use_database']FALSE

這是有效的,因爲當再次請求會話時會自動創建會話,而不是將會話存儲在數據庫中時的工作原理。可能不太安全。

最後,由您來決定哪個選項最適合您的應用程序。

0

沒錯。 CodeIgniter的文檔指定位置: http://codeigniter.com/user_guide/libraries/sessions.html

============================= 
Destroying a Session 

To clear the current session: 
$this->session->sess_destroy(); 

Note: This function should be the last one called, 
    and **even flash variables will no longer be available**. 
    If you only want some items destroyed and not all, use unset_userdata(). 
============================= 

我已經挖進系統/庫/ session.php文件文件並保存flashdata觸發sess_write()方法只如你所說更新數據庫。

0

對我來說,一個更好的解決方案是在show_message()中設置flashdata之前檢查以確保會話存在。

function _show_message($message) 
{ 
    // Patch for show_message() after logout(). Make sure the session exist before set_flashdata(). 
    if(!$this->session->sess_read()) 
    { 
     $this->session->sess_create(); 
    } 
    $this->session->set_flashdata('message', $message);  
    redirect('/auth/');  
}