What is Notice: unserialize() [function.unserialize]: Error at offset then?

Problem: While doing some Shopp Plugin support work earlier, one customer was getting this error…

Notice: unserialize() [function.unserialize]: Error at offset 111 of 118 bytes in /home/maxwell4/public_html/main/wp-content/plugins/shopp/core/model/Settings.php on line 228

…she had done a ‘find and replace’ in a SQL DB dump to change her domain name, boogered it up accidentally, and needed an explanation. The answer I gave applies any time that error occurs in PHP, so i thought it was worth posting here!

Reason: Arrays are often serialized so that they can be stored in a database text field. If you change the data within a serialized array, without changing the character count, you will get the error above.

For example, try this:

$thing = array('one','two');
echo serialize($thing);

This will echo out the following: a:2:{i:0;s:3:”one”;i:1;s:3:”two”;}
Now… if you stored that in a database, you could then retrieve it later and unserialize() it to get your array back. wohoo!

In that serialized data, s:3:”one” means that the first element in the array is a string, and that it’s 3 characters long. Now.. if you manually changed “one” to “otherone” like this a:2:{i:0;s:3:”otherone”;i:1;s:3:”two”;} then when you ran it through unserialize() to retrieve your array, you would get the error were talking about.

Fix: To correct this you would need to update the string length also like so: a:2:{i:0;s:8:”otherone”;i:1;s:3:”two”;} because “otherone” is actually 8 characters long. See? Good!

Any questions, please leave a message below.

Leave a Reply

Your email address will not be published. Required fields are marked *