Set_flashdata有时不能在我的codeigniter应用程序中工作


set_flashdata some times not working in my codeigniter application

我使用codeigniter,在我的控制器我不能设置flashdata,但我可以设置用户数据

 /**
 * this will send the requests for the gmail wrapper
 * @return void
 * @author Sandaruwan
 * */
function send_contacts()
{
    $contacts = $this->input->post('contact');
    if (count($contacts) != 0 && is_array($contacts)) 
    {   
        $data = $this->gmailmanager->send_messeges($contacts);
         echo "error message ===>"; print_r($this->message->get_message ()); echo "<br/>";
        $this->session->set_flashdata('message',$this->message->get_message ());
        echo "flash data ===>"; print_r($this->session->flashdata('message')); echo "<br/>";

        $this->session->set_userdata('user',$this->message->get_message ());
        echo "user data ===>"; print_r($this->session->userdata('user')); echo "<br/>";
        exit;
        redirect('connections/connection_inviter/invite');
    }
    else
    {
        $this->message->set_information(array(_('You have not selected a conatact!')));
        $this->session->set_flashdata('message',$this->message->get_message ());
        redirect('connections/connection_inviter/invite');  
    }
}

这是结果

    error message ===>
    Warning
        Email sending error!

    flash data ===>
    user data ===>
    Warning
        Email sending error
!

问题是我可以得到error messageuserdata,但不能得到flashdata,我不明白为什么初始化后不能得到flashdata

在一些控制器flashdata工作完美。

更新
function invite() {
$this->load->library("connections/Outlookmanager");
print_r($this->ci->session->flashdata('message'); die;
    //Invite friends links        
$this->data['is_windows']=$this->outlookmanager->is_windows_user();
$this->load->view('connections/invite_friends', $this->data);
}

当我在function invite()中打印flashdata时,它不打印。

嗯。其实有一个很有趣的问题,

我有错误信息Email sending error,我把它改成err,现在flash数据工作。我再次将消息更改为Email sending error,它再次不起作用。然后我再次将其更改为err,然后再次flashdata工作。

这是什么,我认为错误消息长度导致这里的问题,我不知道为什么

这是因为flash数据实际上是cookie(尽管CI使用"session"一词),因此它们仅在下一次请求时可用。

老实说,我不明白为什么要在创建后立即设置和调用flashdata,为什么不直接打印出错误并跳过这个无用的步骤呢?

如果你仍然想要两者,就做

$this->session->set_flashdata('message',$this->message->get_message());
echo $this->message->get_message();

这样你现在就有了一个消息,之后你发出的任何请求都有一个消息

更新:

尝试调用$this实例的方法而不是$this->ci

print_r($this->session->flashdata('message');

我想我已经看到你这样做了,有时使用$this的参考,有时使用另一个对象,IIRC我甚至回答了一个关于差异的问题。为什么呢?为什么在一个位置使用$this,然后在另一个位置使用$this->ci-> ?这可能没有什么区别,但至少为了一致性,坚持使用一种方式(我建议使用"常规"方式)