Wouter Florijn
Wouter Florijn

Reputation: 2941

CodeIgniter flash data working locally but not on server

Hello CodeIgniter users.

I have a problem with flash data and I would like some help. My CI version is 2.1.4.

I am using CI flash data to store data temporarily for a form that consists of multiple pages. Data entered on each page is stored so it can be accessed on the next pages and finally all data is entered int the database.

Now to keep data stored through multiple pages, instead of only one, I extended the Session class with the following function:

function keep_all_flashdata($prefix = '')
{
    $userdata = $this->all_userdata();
    foreach ($userdata as $key => $value)
    {
        if (strpos($key, ':old:' . $prefix))
        {
            $new_flashdata_key = str_replace(':old:', ':new:', $key);
            $this->set_userdata($new_flashdata_key, $value);
        }
    }
}

This function preserves all flash data (or optionally only flash data that starts with a certain string) for another redirect. It is similar to the keep_flashdata function except for the fact that it works for multiple items without requiring their exact name.

After calling this function, both :old: and :new: keys are stored in the session data. Then after a redirect, old keys are removed and new keys are set to old. Then, if there's another page, I call keep_all_flashdata() again and so on until the last page.

This works fine when I'm working on my local WAMP server, but on my actual server, all flashdata just gets removed after a redirect, even if it has :new: in the key. I confirmed my keep_all_flashdata() function works by checking the contents of session->all_userdata() and everything looks as expected.

I am using some AJAX calls, but they should not erase flash data (a known issue) as I've prevented this with $this->CI->input->is_ajax_request() before flashdata is cleared (in the sess_update() and _flashdata_sweep() functions).

Is this a bug in CodeIgniter or am I doing something wrong? Any help is appreciated.

Upvotes: 1

Views: 409

Answers (1)

Ron Kösters
Ron Kösters

Reputation: 1

I think your if statement is causing the problem. I'm assuming that ":old:" or ":new:" is used as prefix for every key you store in a session?

strpos() returns the position of where the needle exists so that would be 0 when checking a key with the prefix ':old:'. That's intended as old flashdata needs to be removed. I tested the following piece of code:

$flashDataKey = ':new:myKey';

die(var_dump(strpos($flashDataKey, ':old:')));

Which returns false as expected since the needle was not found. Resulting in not storing the flashdata as ':old:' and keeping it for the next request.

I'm not sure why this is working on your localhost. You should change your if statement to:

if( strstr($key, ':new:') !== false)

Now only keys containing the string ':new:' will pass and everything else will return false. Hope this helped!

Upvotes: 0

Related Questions