CodeIgniter (&PHP) Serialize() & Unserialize() Issues

I’ve been working on a project which includes large forms and big data sets and realized that for few accounts customer was unable to login.

I checked and checked again but login was working fine and I was wondering what could be the issue.

This is what I found in CodeIgniter Logs :

Severity: Notice  --> unserialize() [function.unserialize]: Error at offset 214 of 255 bytes

On further investigation I found that CodeIgniter is using  the serialize() and unserialize() functions at time of Session storage.

Serialize allows us to keep an array or object in a text form (as a long string). When assigning arrays to things like $_SESSION, it allows PHP to store it in a text file, and then recreate it later. Serialize is used like this for objects and variables. The above error was coming when CodeIgniter was trying to unserialize on some information which was previously serialized.

It turns out that if there’s a <strong>:</strong> in one of  the array values and because of which the serialization was getting corrupted.
Other symbols which can result in corrupted string after serialization are " , ' , : , or ;

After some search I’ve found the small fix for this issue on PHP.net:

//To serialize $resultstring = base64encode(serialize($array)); /To unserialize $restoredarray = unserialize(base64decode($result_string ));

So, finally its working.