2011-03-16 54 views
4

我在CI 1.7.3 App中使用Tank Auth進行用戶管理。一切工作正常,但我試圖設置一個flash_message當用戶註銷時顯示。問題是$this->tank_auth->logout();函數會破壞會話。我已經修改了坦克驗證庫的註銷功能看起來像:銷燬會話但保留Flashdata

function logout() { 
     $this->delete_autologin(); 

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

這是以前

function 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(); 
     } 

在我的控制器我有

function logout(){ 
    if ($this->tank_auth->is_logged_in()) { // logged in 
     $this->session->set_flashdata('status_message', $this->lang->line('auth_message_logged_out')); 
     $this->tank_auth->logout(); 

     redirect('');   

    } 

}

如果我刪除$this->tank_auth->logout();函數消息顯示罰款。我敢肯定,這是一個簡單的會話問題

回答

1

雖然這是一種解決方法,它可能會爲你做的伎倆......

無論你展示這些,我會假設你在檢查該視圖,使...

<? if ($this->session->flashdata('status_messege'): ?> 

    <p><?= $this->session->flashdata('status_message') ?></p> 

<? endif; ?> 

COULD的ELSEIF添加到和檢查引薦是你的註銷功能...

<? if ($this->session->flashdata('status_messege'): ?> 

    <p><?= $this->session->flashdata('status_message') ?></p> 

<? else if ($this->agent->referrer() == site_url('path/to/logout'): ?> 

    <p><?= $this->lang->line('auth_message_logged_out') ?></p> 

<? endif; ?> 

克服這個問題的方法有些詭異,但可能還是一種方法。

2

sess_destroy()函數也銷燬用於傳遞消息的會話閃存變量。

ü已經回答了你的問題,在圖書館logout()功能,則需要更換

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

$this->ci->session->unset_userdata(array('user_id' => '', 'username' => '', 'status' => '')); 

這不會完全破壞了會議,僅用於登錄的用戶數據,所以我建議您修改控制器中的logout()函數,並手動顯示消息,方法是將其傳遞給視圖。

6

如果您在撥打sess_destroy()後嘗試在同一個請求中使用數據庫時設置了flashdata,那麼它將不起作用(因爲沒有會話將追加flashdata)。

要解決此問題,請在致電sess_destroy()後添加$this->ci->session->sess_create();。這可以起作用,因爲在嘗試將數據附加到它之前,您正在重新創建會話。如果您在數據庫中使用會話,這是在sess_destroy()之後使用flashdata的唯一方法。

+0

感謝您的提示! – dennisbot 2013-07-13 13:29:21