销毁Session但保留flashdata

发布于 2024-10-22 20:50:26 字数 1506 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相权↑美人 2024-10-29 20:50:26

如果您在调用 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 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.

过气美图社 2024-10-29 20:50:26

sess_destroy() 函数还会销毁用于传递消息的会话闪存变量。

你已经回答了你的问题,在库 logout() 函数中,你需要替换

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

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

这不会完全破坏会话,只有用于登录的用户数据,所以我建议改为修改控制器中的 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 replace

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

with

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

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.

权谋诡计 2024-10-29 20:50:26

虽然这是一种解决方法,但它可能对您有用...

无论您在何处显示这些内容,我都会假设您正在检查视图,因此...

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

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

<? endif; ?>

可以添加一个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; ?>

克服此问题的有点黑客方法,但可能仍然是一种方法。

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...

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

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

<? endif; ?>

you COULD add an elseif to that and check for the referrer being your logout function...

<? 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; ?>

A bit of a hackish way to overcome this issue, but probably a way nonetheless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文