Welcome Guest, Not a member yet? Register   Sign In
Unexpected flashdata behavior
#1

[eluser]Jeka[/eluser]
Hi,

I've run into this weird problelm with flashdata. Code snippet:

Code:
function edit_entry($id) {
    $this->data['entry'] = $this->ion_auth->get_user($id);//get the user
    if (!$this->data['entry']) { //bail if can't retrieve
        $this->session->set_flashdata('status', 'error');
        $this->session->set_flashdata('message', "Unable to load this entry for edit");    
        redirect('admin/manage_entries', 'refresh');
    }
    $this->load->view('admin/edit_entry',$this->data);    
}

Here's what happens:

If the user can't be retrieved, it's supposed to refresh away.

However, the flash data is being set without redirect. So every time I refresh the edit_entry page, I get an error message - even if the record is retrieved successfully. I get that exact error message, it's the only place in code it's ever set. If I comment out the set_flashdata then it obviously never sets and the page works fine.

Does anyone know what can be causing this? It doesn't make sense to me: if we ever got to setting flash data, it would redirect. If not, then the flash data shouldn't be set. So here I get no redirect and the flash data is being set.

Edit: also, if I pass an incorrect ID, it properly redirects.
#2

[eluser]mddd[/eluser]
The session cookie functions and the redirect function both use HTTP headers. Maybe they are getting in each others way somehow.
I have found some documentation saying that you should put out a redirect header BEFORE setting the cookies. In that case, CI's redirect() will not work. You could try setting the headers through PHP's header() function:
Code:
header('Location: '.site_url('admin/manage_entries');
$this->session->set_flashdata('status', 'error');
$this->session->set_flashdata('message', "Unable to load this entry for edit");    
exit();
#3

[eluser]Jeka[/eluser]
Well, I'll be damned. The way you described works perfectly.

I'm still weirded out though, considering that the inside of the IF statement shouldn't have even executed to begin with.

Thanks for your help!
#4

[eluser]mddd[/eluser]
You're welcome. HTTP headers are often a bit wacky.

About the IF condition: check what your get_user() method is returning. Maybe in some circumstance it is returning something that does not evaluate to 'false'?
#5

[eluser]Jeka[/eluser]
[quote author="mddd" date="1276808602"]You're welcome. HTTP headers are often a bit wacky.

About the IF condition: check what your get_user() method is returning. Maybe in some circumstance it is returning something that does not evaluate to 'false'?[/quote]

Under which conditions do you think the statement would set the flash variable but not redirect?

I just tried if (false) and you're right, it works okay then.

What's the best way to test for this in your opinion?
#6

[eluser]mddd[/eluser]
Well, $this->data['entry'] is set to the result of get_user($id);
You've said, if you supply an incorrect user id, it does work (in other words: it returns false).
But what are other possible return values. Perhaps if it doesn't find anything you expect it to return false but maybe it returns an empty array or something. That is not 'false' so it would not match your 'if' condition..
So I would suggest checking out what return values you get for different situations, to understand what is happening.




Theme © iAndrew 2016 - Forum software by © MyBB