使用 Tank Auth 时,在重定向之间不会存储 flashdata
我正在构建的网站上使用最新版本的 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题并找出了问题所在。如果您将会话存储在数据库中,它将不起作用。
Tank Auth 从主库运行此代码 (
$this->tank_auth->logout()
):然后它从身份验证控制器运行此代码 (
$this->_show_message ()
):问题在于,由于
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()
):Then it runs this code from the auth controller (
$this->_show_message()
):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 functionlogout()
inapplication/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 functionlogout()
inapplication/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']
toFALSE
.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.
如果 Tank_auth 执行任何内部重定向,那么您可能会丢失该重定向请求上的闪存数据。
if tank_auth does any internal redirects then you may lose the flash data on that redirect request.
确切地。
CodeIgniter 文档在此处指定:
http://codeigniter.com/user_guide/libraries/sessions.html
我已经挖掘过了进入 system/libraries/Session.php 文件并保存 flashdata 会触发 sess_write() 方法,该方法仅按照您所说的方式更新数据库。
Exactly.
CodeIgniter documentation specifies here:
http://codeigniter.com/user_guide/libraries/sessions.html
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.对我来说,更好的解决方法是在 show_message() 中设置 flashdata 之前检查以确保会话存在。
To me a better fix is checking to make sure the session exist before setting the flashdata in show_message().