销毁Session但保留flashdata
我在 CI 1.7.3 应用程序中使用 Tank Auth 进行用户管理。一切工作正常,但我试图设置一个在用户注销时显示的 flash_message
。问题是 $this->tank_auth->logout();
函数破坏了会话。我已将 Tank Auth 库中的注销函数修改为如下所示:
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();
函数消息显示正常。我确定这是一个简单的会话问题
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);
}
It was previously
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();
}
In My controller I have
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('');
}
}
If I remove the $this->tank_auth->logout();
function the message shows fine. I'm sure it's a simple session problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在调用
sess_destroy()
后在同一请求中使用数据库时尝试设置 flashdata,则该操作将不起作用(因为没有会话可以附加 flashdata)。要解决此问题,请在调用
sess_destroy()
之后添加$this->ci->session->sess_create();
。这是有效的,因为您在尝试向会话追加数据之前要重新创建会话。如果您在数据库中使用会话,这是在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).To fix this problem, add
$this->ci->session->sess_create();
after the call tosess_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 asess_destroy()
if you're using sessions in a database.sess_destroy()
函数还会销毁用于传递消息的会话闪存变量。你已经回答了你的问题,在库
logout()
函数中,你需要替换为
这不会完全破坏会话,只有用于登录的用户数据,所以我建议改为修改控制器中的
logout()
函数并通过将消息传递给视图来手动显示消息。The
sess_destroy()
function destroys also the session flash variables used to pass the message.U already answered your question, in the library
logout()
function, you need to replacewith
This will not completely destroy the session, only the user data used for login, so I recommend instead, to modify the
logout()
function in the controller and show the message manually, by passing it to a view.虽然这是一种解决方法,但它可能对您有用...
无论您在何处显示这些内容,我都会假设您正在检查视图,因此...
您可以添加一个elseif 并检查引荐来源网址是否为您的注销功能...
克服此问题的有点黑客方法,但可能仍然是一种方法。
While this is a workaround, it might do the trick for you...
wherever you're displaying these, I'll be assuming you're checking in the view so...
you COULD add an elseif to that and check for the referrer being your logout function...
A bit of a hackish way to overcome this issue, but probably a way nonetheless.