Destroy Session但保留flashdata [英] Destroy Session but keep flashdata
问题描述
我在我的CI 1.7.3应用程式中使用 Tank Auth 进行用户管理。一切正常工作,但我试图设置 flash_message
将显示,当用户注销。问题是 $ this-> tank_auth-> logout();
函数销毁会话。我修改了在Tank Auth库的注销功能,看起来像:
I am using Tank Auth for user management in my CI 1.7.3 App. Everything is working fine but I'm trying to set a flash_message
to be displayed when the user logs out. The problem is the $this->tank_auth->logout();
function destroys the session. I have modified the logout function in the Tank Auth library to look like:
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();
函数,消息显示正常。我确定这是一个简单的会话问题
If I remove the $this->tank_auth->logout();
function the message shows fine. I'm sure it's a simple session problem
推荐答案
如果您尝试在同一请求中使用数据库时设置flashdata调用 sess_destroy()
,它将不工作(因为没有会话来追加flashdata)。
If you try to set flashdata while using a database in the same request after you call sess_destroy()
, it won't work (because there is no session to append the flashdata to).
为了解决这个问题,在调用 sess_destroy()之后添加
。这是因为您在尝试向其附加数据之前重新创建会话。如果您在数据库中使用会话,这是在 $ this-> ci-> session-> sess_create();
sess_destroy()
后使用flashdata的唯一方法。
To fix this problem, add $this->ci->session->sess_create();
after the call to sess_destroy()
. This works because you're re-creating the session before trying to append data to it. This is the only way to use flashdata after a sess_destroy()
if you're using sessions in a database.
这篇关于Destroy Session但保留flashdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!