使用 Tank Auth 时,在重定向之间不会存储 flashdata

发布于 2024-12-07 04:46:55 字数 197 浏览 1 评论 0原文

我正在构建的网站上使用最新版本的 Codeigniter 和 Tank_auth 1.0.9。

分别使用 set_flashdata() 和 flashdata() 时,重定向时不会返回任何内容,但如果我在配置中将 sess_use_database 设置为 FALSE,则它可以工作。

我四处搜寻,找不到答案——还有其他人遇到这个问题并解决了吗?

I'm using the latest version of Codeigniter and tank_auth 1.0.9 on a site I'm building.

When using set_flashdata() and flashdata() respectivly, nothing is being returned on redirect but if I set sess_use_database to FALSE in the config it works.

I've searched around and couldn't find an answer -- Has anyone else run into this issue and fixed it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

橘香 2024-12-14 04:46:55

我遇到了同样的问题并找出了问题所在。如果您将会话存储在数据库中,它将不起作用。

Tank Auth 从主库运行此代码 ( $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_create(); this->ci->session->sess_destroy();application/libraries/Tank_auth.php 的函数 logout()

这是有效的,因为您正在创建一个可以存储闪存数据的新空白会话。这样做的一个潜在缺点是您要对数据库执行更多操作(删除+插入)。

选项 2:

注释掉/删除函数 logout() 中的 $this->ci->session->sess_destroy();application/libraries/Tank_auth.php 中,

这是有效的,因为会话没有被破坏,允许 CI 仅执行更新查询来添加 flashdata。这可能比选项 1 更好,除非您绝对需要销毁会话。

选项 3

$config['sess_use_database'] 设置为 FALSE

这是有效的,因为再次请求时会自动创建会话,这与将会话存储在数据库中时的工作方式相反。可能不太安全。

最后,由您决定哪个选项最适合您的应用。

I was having the same issue and figured out the problem. If you're storing sessions in the database, it will not work.

Tank Auth runs this code from the main library ( $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();

Then it runs this code from the auth controller ( $this->_show_message() ):

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

The problem is that since sess_destroy() was run prior to setting the flashdata, there is no database row to add the flashdata to, so the flashdata never gets set.

At this point there are a few solutions:

Option 1:

Add $this->ci->session->sess_create(); immediately after $this->ci->session->sess_destroy(); in function logout() in application/libraries/Tank_auth.php

This works because you are creating a new blank session where flashdata can be stored. A potential con for this is that you are performing more operations on the database (delete+insert).

Option 2:

Comment out/delete $this->ci->session->sess_destroy(); in function logout() in application/libraries/Tank_auth.php

This works because the session is not destroyed, allowing CI to perform only an update query to add flashdata. This is probably better than option 1 unless you absolutely need to destroy the session.

Option 3:

Set $config['sess_use_database'] to FALSE.

This works because a session is automatically created when it is requested again, as opposed to how it works when you store sessions in the database. Potentially less secure.

In the end, it is up to you to decide which option is best for your application.

ゝ杯具 2024-12-14 04:46:55

如果 Tank_auth 执行任何内部重定向,那么您可能会丢失该重定向请求上的闪存数据。

if tank_auth does any internal redirects then you may lose the flash data on that redirect request.

会傲 2024-12-14 04:46:55

确切地。
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().
=============================

我已经挖掘过了进入 system/libraries/Session.php 文件并保存 flashdata 会触发 sess_write() 方法,该方法仅按照您所说的方式更新数据库。

Exactly.
CodeIgniter documentation specifies here:
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().
=============================

I've digged into the system/libraries/Session.php file and saving flashdata triggers the sess_write() method which only UPDATES the database as you said.

恰似旧人归 2024-12-14 04:46:55

对我来说,更好的解决方法是在 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/');      
}

To me a better fix is checking to make sure the session exist before setting the flashdata in show_message().

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