为什么 CodeIgniter 中的 flashdata 并不总是有效而 userdata 总是有效?

Why flashdata in CodeIgniter does not always work whereas userdata always works?

我有一个代码块用于为 report 控制器中的 flashdata 赋值,另一个代码块用于访问 export_data 控制器中的 flashdata

Report 控制器:

if ($this->input->get_post('date_frm')) {
              $conditions[] = 'appointment_date >= "'.trim($this->input->get_post('date_frm', TRUE)).'"';
            }

            if ($this->input->get_post('date_to')) {
              $conditions[] = 'appointment_date <= "'.trim($this->input->get_post('date_to', TRUE)).'"';
            }

            $conditions = $this->search_model->searchterm_handler($conditions);

            $this->session->set_flashdata('ext_data', $conditions);

并且在 Export_data 控制器中:

$myVar = $this->session->flashdata('ext_data');
    $this->session->keep_flashdata('ext_data');

它并不总是有效,但是当我使用 userdata 而不是 flashdata 时它工作正常。为什么?

OP: when I use userdata rather than flashdata it is working fine

flashdata()的数据集仅保存一次一次性请求,然后清除:

Documentation: CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared.

...并且 keep_flashdata() 仅在第一个请求之后为 一个额外的 请求保存数据:

Documentation: If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() method.


但是,在会话被销毁之前,userdata() 始终可用。

参见:What is Session Data?


我认为您应该阅读完整的文档:Session Library

首先你应该知道什么是flashdata

它不清除会话值时 called.Even 它可用于下一次服务器调用,但不会用于第二次调用。

举个例子,假设您在控制器中有两个函数

function test1()
{
    $this->session->set_flashdata('ext_data', 'test');
}
function test2()
{
    echo $this->session->userdata('ext_data');
    echo $this->session->userdata('ext_data');
    echo $this->session->userdata('ext_data');
}

现在,如果您调用 your_site_url/controller/test1,它将为 ext_data

设置 test

之后,如果您调用 your_site_url/controller/test2,它将打印 test 字词 3 次,这意味着您这次可以根据需要使用此会话变量,但下次调用(再次点击 your_site_url/controller/test2) 它将是空白的。

希望你明白这是什么意思。

CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared.