CodeIgniter:设置闪存数据不工作 [英] CodeIgniter: setting flash data not working

查看:111
本文介绍了CodeIgniter:设置闪存数据不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  if($ this-& > 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'] )):
...

所有这些在我的开发计算机上都可以正常工作,窒息在现场网站上;最后的 if()语句不会计算为真。



$ c> $ post ['search-notes'] 变量在最后 if()语句之前或之内, >

这是非常奇怪的,我从来没有遇到过类似的事情。



我使用CodeIgniter 2.0

在旁注中,原标题有更多的特异性: set_flashdata()在CodeIgniter中的问题。

解决方案

第一个你应该注意的事情是一旦你调用 $ this-> session-> flashdata('search-notes')方法,它会取消



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



如果您要保持项目在会话中,请使用 set_userdata() userdata()



此外,您可以在之后使用 keep_flashdata('search-notes') set_flashdata / code>之前调用 flashdata()以保存flashdata变量。



作为侧面点

无需选中 isset()!empty()在一起。 empty() 不生成如果变量不存在,则返回警告,并返回 FALSE



CI参考



还有一个不错的




Jus作为演示:

strong>

不要复制,请检查逻辑。

  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 item in session,请在第一个 if 语句中使用以下语句:

  if($ _POST ['search-notes'] AND is_string($ _ POST ['search-notes']))
{
$ post ['search- notes'] = $ this-> input-> post('search-notes'/ *,TRUE * / / *启用XSS过滤*
$ this-> session-> set_flashdata('search-notes',$ post ['search-notes']);
//通过额外的请求保存flashdata
$ this-> session-> keep_flashdata('search-notes');

} // ...


I'm using the following code to manage pagination in search results:

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'])):
...

All of which works fine on my development computer, but chokes on the live website; the final if() statement does not evaluate to true.

However, if I echo out the $post['search-notes'] variable either before or within the final if() statement, it works!

It's utterly bizarre, and I've never encountered anything like it before.

I'm using CodeIgniter 2.0

On a side note, the original title had much more specificity: "Problem with set_flashdata() function in CodeIgniter". But due to some excitable and excessive moderation rules, I've had to water it down to something less meaningful.

解决方案

The first thing you should attend is once you call $this->session->flashdata('search-notes') method, it unsets the 'search-notes' item from session.

So, when you check the $this->session->flashdata('search-notes') at the second time, 'search-notes' will no longer exists.

If you want to keep the item in session, use set_userdata() and userdata() instead.

Also, you can use keep_flashdata('search-notes') after set_flashdata() or before the first call of flashdata() to preserve the flashdata variable through an additional request.

As a side point:
There's no need to check isset() and !empty() together. empty() does NOT generate a warning if the variable does not exist, and returns FALSE.

CI reference

There is also a nice tutorial on nettuts+ could be useful.


Jus as a Demo:
do NOT copy, check the logic.

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'])):
// ...

If you need to keep the search-notes item in session, use the following at the first if statement:

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');

} // ...

这篇关于CodeIgniter:设置闪存数据不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆