Flash数据消息未显示在视图中

I am trying to set flash data in Controller 1 and redirect to Controller 2, where I store the data in a $page_data variable to get it displayed in View 2. The following are my codes:

Controller 1:

$this->session->set_flashdata('message','my custom message');
  redirect('controller2','refresh');

Controller 2:

 $page_data['loginmessage'] = $this->session->flashdata('message');
 $this->load->view('view2',$page_data);

View 2:

<p> <?php echo $loginmessage ?> </p>

CodeIgniter is able to load View 2, but does not display the login message. To put in another way, I am not getting the flash data message in Controller 2.

You need to change the small piece of code your code to redirect it correctly.

Controller 1:

function first()
{
$this->session->set_flashdata('message','my custom message');
redirect("/controller2/second", "refresh");
}

Controller 2:

function second()
{
$page_data['loginmessage'] = $this->session->flashdata('message');
$this->load->view('view2',$page_data);
}

Also, if you refresh the page then flash data will disappear because it will appear only once.

View code is same

<p> <?php echo $loginmessage ?> </p>

No need to use controller 2 for showing flashdata. Use this

Controller1

  $this->session->set_flashdata('message','my custom message');
  redirect('controller2/any_method');

Now you may use message in view2.php (which you are loading through controller 2) file as

<p> <?php echo $this->session->flashdata('message'); ?> </p>