CodeIgniter:设置闪存数据不起作用


CodeIgniter: setting flash data not working

我使用以下代码来管理搜索结果中的分页:

if ($this->input->post('search-notes') && (is_string($this->input->post('search-notes')) || is_string($this->input->post('search-notes')))):
    $this->session->set_flashdata('search-notes', $_POST['search-notes']);
    $post['search-notes'] = $this->input->post('search-notes');
elseif ($this->session->flashdata('search-notes')):
    $this->session->set_flashdata('search-notes', $this->session->flashdata('search-notes'));
    $post['search-notes'] = $this->session->flashdata('search-notes');
endif;
if (isset($post['search-notes']) && is_string($post['search-notes']) && !empty($post['search-notes'])):
...

所有这些在我的开发电脑上都能很好地工作,但在直播网站上却会窒息;则最终的CCD_ 1语句的评估结果不为真。

但是,如果我在最后的if()语句之前或之内回显$post['search-notes']变量,它就可以工作了!

这太奇怪了,我以前从来没有遇到过这样的事情。

我正在使用CodeIgniter 2.0

附带说明一下,原标题有更多的特殊性:"CodeIgniter中set_flashdata()函数的问题"。但由于一些容易激动和过度节制的规则,我不得不把它淡化为一些没有意义的东西。

您应该参与的第一件事是,一旦调用$this->session->flashdata('search-notes')方法,它就会从会话中取消设置'search-notes'项。

因此,当您第二次检查$this->session->flashdata('search-notes')时,'search-notes'将不再存在。

如果要将项目保留在会话中,请改用set_userdata()if()0。

此外,您可以在set_flashdata()之后使用keep_flashdata('search-notes'),或在第一次调用flashdata()之前使用,以通过附加请求保留flashdata变量。

作为旁注:
不需要同时检查isset()!empty()。如果变量不存在,empty()不会生成警告,并返回FALSE

CI参考

还有一个关于netuts+的不错的教程可能会很有用。


Just作为演示:
不要复制,检查逻辑

if ($_POST['search-notes'] AND is_string($_POST['search-notes']))
{
    $post['search-notes'] = $this->input->post('search-notes'/*, TRUE*/ /* Enable XSS filtering */);
    $this->session->set_flashdata('search-notes', $post['search-notes']);
}
elseif ($searchNotes = $this->session->flashdata('search-notes'))
{
    $post['search-notes'] = $searchNotes;
}
if (! empty($post['search-notes']) AND is_string($post['search-notes'])):
// ...

如果您需要在会话中保留search-notes项,请在第一个if语句中使用以下内容:

if ($_POST['search-notes'] AND is_string($_POST['search-notes']))
{
    $post['search-notes'] = $this->input->post('search-notes'/*, TRUE*/ /* Enable XSS filtering */);
    $this->session->set_flashdata('search-notes', $post['search-notes']);
    // Keep the flashdata through an additional request
    $this->session->keep_flashdata('search-notes');
} // ...