How to keep flash data in Codeigniter?
I've a controller with a method there it catch an argument and set it in a flash var.
The question is...if i send many arguments to this method, using multiple firefox 开发者_StackOverflow中文版tabs but the same user session, could them get mixed or rewrited?
I mean, if i open a tab and send the "x" argument and is saved in a flash var and in another tab i send the argument "y" and is "x" rewrited by "y" or is handled like two different things? (i'm using stored sessions)
Also i'm having troubles keeping flash data. It don't keep for next use if i change between sections. If i go to modify/general the flashdata is deleted (replaced by 0).
My code:
function modify(){
$section = $this->uri->segment(3);
switch ($section) {
case 'identity':
$this->session->keep_flashdata('item');
$this->_modify_identity();
break;
case 'general':
$this->session->keep_flashdata('item');
$this->_modify_general();
break;
case 'print':
echo $this->session->flashdata('item');
break;
default:
if(is_numeric($section)){
$this->session->set_flashdata('item', $section);
}
redirect('modify/identity');
break;
}
}
IMHO, flashdata it's meant to be used to pass feedback after an action. The user submits a form or clicks on some action, the app process the request and set a flashdata as a response like "Success" or "Fail". It's meant to be used right after the request, in the next page load.
With that in mind, it's pretty unlikely your user can submit two forms, on two tabs, at the same time. They responses can't be overwritten.
Also, on a side note, I've found that flash data work great using redirect with "refresh" as a second parameter. Otherwise, sometimes it fails.
The $this->session->keep_flashdata($item)
method will only keep the specified element. With this function CI should keep all items for the next page request:
foreach($this->session->all_userdata() as $key => $val){
if(strpos($key,'flash:old:') > -1){ // key is flashdata
$item = substr($key , strlen('flash:old:'));
$this->session->keep_flashdata($item);
}
}
Sessions carry across tabs. On way to check this is by logging out from your CI app in one tab, and see that all others tabs ask you to re-authenticate.
The set_flashdata method is not meant to be a persistent datastore in a session. It is meant to carry things like success and error messages across pages. You should user the set_userdata method for data that should persist throughout the session (or until you decide to delete it).
精彩评论