Archive for July 8, 2011

Serialize and Unserialize using base64

I’ve encounter error at offset when using unserialize.

Notice: unserialize(): Error at offset 2 of 52 bytes in file.php on line 8881

So here’s the solution:

If you want to bring multi dimensional array to the next function , you have to serialize it and write to cookie .
This is how you can do it safely without losing offset.

//securely serialize your data
$this->Cookie->write('nama_data_set', base64_encode(serialize($this->data)),false);
 
//how to securely unserialize it on another function
$data_yang_dibaca = unserialize(base64_decode($this->Cookie->read('nama_data_set')));