【问题标题】:flashdata not being stored between redirects when using Tank Auth使用 Tank Auth 时,未在重定向之间存储 flashdata
【发布时间】:2011-09-30 01:01:03
【问题描述】:

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

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

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

【问题讨论】:

  • 您确定这与 Tank_Auth 有关吗?尝试过全新安装?
  • 我建议放置 log_message(..) 语句并检查浏览器发出的每个 http 请求的 flashdata 的值。 codeigniter 中的内部重定向(使用redirect())也会导致flashdata 被清除。检查您的日志文件是否存在可能是原因的错误重定向()

标签: codeigniter tankauth


【解决方案1】:

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

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:

application/libraries/Tank_auth.php中的函数logout()中的$this->ci->session->sess_destroy();之后立即添加$this->ci->session->sess_create();

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

选项 2:

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

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

选项 3:

$config['sess_use_database'] 设置为FALSE

之所以有效,是因为会话在再次请求时会自动创建,而不是在您将会话存储在数据库中时它的工作方式。可能不太安全。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      没错。 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() 方法,该方法仅如您所说的那样更新数据库。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-25
          • 1970-01-01
          • 2017-07-21
          • 2016-05-13
          • 2012-09-03
          • 2022-01-24
          相关资源
          最近更新 更多