Flashdata not getting cleared in Codeigniter

让人想犯罪 __ 提交于 2019-12-24 16:35:25

问题


I am using Codeigniter 2.1.4 and I have facing some issues with flashdata. When I successfully submit record I can display the flashdata message. But if go to the other page from the page where flashdata message was displayed and then go back to previous page using browser back button it shows me flashdata message again.
How to clear flashdata message once it used? I think its not the flashdata issue its cache problem. I am confused why this is happening. If its cache issue then how to remove it?

Below is code I have used,

//In the manage of controller
$this->session->set_flashdata('message', "Record updated successfully.");

// In the view of controller
$data['message'] = $this->session->flashdata('message');

// In the view page
echo $message;

回答1:


your code in controller is right

//In the manage of controller
$this->session->set_flashdata('message', "Record updated successfully.");
redirect('controller_name/function_name','refresh');

now in view use like this

if($this->session->flashdata('message')){echo $this->session->flashdata('message');}

hope it will work




回答2:


Flash disappears only after next refresh




回答3:


if you want to clear set_flash in controller or another view file, then you can use this simple code.

$this->session->set_flashdata('error', 'User not found...'); //create set_flash

destroy set_flash

//echo "<pre>"; print_r($_SESSION); die; //for check 

if(isset($_SESSION['error'])){
    unset($_SESSION['error']);
}



回答4:


$this->session->set_flashdata('message', "Record updated successfully.");

After setting the flashdata redirect to some function or to the same function.




回答5:


You must redirect the page somewhere after $this->session->set_flash('item','value');

Example:

if ($this->form_validation->run() == FALSE){
    $this->session->set_flashdata('error',validation_errors());
    redirect(base_url().'user/login');
}
else{
    $this->session->set_flashdata('success','Thank you');
    redirect(base_url().'user/login');
}

Usually developer make a mistake when they submit data to same page. They set flash data but forget to redirect.




回答6:


If you refresh in the same controller function the flashdata won't be deleted.Also going back and forth in the browser does't affect the flashdata. to clear the flashdata redirect to another controller function and it will work.



来源:https://stackoverflow.com/questions/18846462/flashdata-not-getting-cleared-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!