Roopa
Roopa

Reputation: 159

Weird session behaviour in codeigniter

Following code works fine ..

$somearray = getData();

$data = array(
    'user_display_name' => $userdisplayname,
    'username'  => $usernamefromdb,
    'logged_in'  => TRUE,
);
$this->session->set_userdata($data); // used to create user session

This works fine with codeigniter and sqlite..

But when i code

$data = array(
    'user_display_name' => $userdisplayname,
    'username'  => $usernamefromdb,
    'logged_in'  => TRUE,
    'arrdata' => $somearray
);

$this->session->set_userdata($data); // used to create user session

It says session terminated... What can be the issue?

When i do var_dump($somearray) it shows the info. Is there any memory limit for sessions??

Thanks

Upvotes: 2

Views: 1118

Answers (2)

Alexey Gerasimov
Alexey Gerasimov

Reputation: 2141

I noticed the same issue in one of my applications too. Debugging the problem lead me to discover that CodeIgniter is (was) not properly implementing serialization/unserialization of multi-dimensional arrays. There was actually a bug submitted for that, and I believe they either fixed it or about to fix it. Take a look at the their session lib in Core.

function _serialize($data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $val)
        {
            if (is_string($val))
            {
                $data[$key] = str_replace('\\', '{{slash}}', $val);
            }
        }
    }
    else
    {
        if (is_string($data))
        {
            $data = str_replace('\\', '{{slash}}', $data);
        }
    }

    return serialize($data);
}

Notice that it only goes through 1 level of your array and that the ONLY level that gets the slashes replaced. The problem is that you have multi-dimensional array and there's probably data there that's throwing off serialization and wiping your session. We fixed it by extending their session library and creating these calls:

class MY_Session extends CI_Session {

    public function __construct()
    {
        parent::__construct();
    }

    function _serialize($data)
    {
        $data = $this->_serialize_backslash_recursive($data);

        return serialize($data);
    }

    function _unserialize($data)
    {
        $data = @unserialize(strip_slashes($data));

        return $this->_unserialize_backslash_recursive($data);
    }

    function _serialize_backslash_recursive($data)
    {

        if (is_array($data))
        {
            return array_map(array($this,'_serialize_backslash_recursive'), $data);
        }
        else
        {
            if (is_string($data))
            {
                return str_replace('\\', '{{slash}}', $data);
            }
        }

        return $data;

    }

    function _unserialize_backslash_recursive($data)
    {

        if (is_array($data))
        {
            return array_map(array($this,'_unserialize_backslash_recursive'), $data);
        }
        else
        {
            if (is_string($data))
            {
                return str_replace('{{slash}}', '\\', $data);
            }
        }

        return $data;

    }

}   

This will now go through all the levels properly. Give it a try and see if it works for you.

Upvotes: 4

Shivaas
Shivaas

Reputation: 694

Codeigniter does not store all the session data in the Database natively (unlike what its makes you believe). It stores the data in the cookie which is limited to 4KB as pointed out by Jose Adrian.

To store all your session data in the DB, use this replacement session class: http://codeigniter.com/wiki/OB_Session/

Storing session data in the database will also be more secure than storing it in a cookie.

Upvotes: 1

Related Questions