Wednesday, 20 May 2009

Drupal 5: Problems with unserialize in

Today I had a problem where Drupal 5 kept reporting a problem with unserialize in on line 428.

On closer inspection,, which is in the includes directory in the root of Drupal 5, contains a number of functions that are used when Drupal 'boots up'. The function in question here was variable_init(), and this is where all variables are drawn from the database. These variables are in serialized form, allowing Drupal to store anything, from objects to arrays, in string format.

If the serialized item got corrupted somehow, it wouldn't be able to unserialize properly in variable_init(), leading to this error. My problem was that I was not able to see which variables were causing the problem; only that the problem existed. With over 200 variables on the site, manually checking each one for valid serialization was not a viable option!

My solution was to change the core to print the names of the offending variables, thereby enabling me to find them in the database and fix them. Here's the original snippet from line 427 of

while ($variable = db_fetch_object($result)) {
$variables[$variable->name] = unserialize($variable->value);

Here's what I changed it to, temporarily:

while ($variable = db_fetch_object($result)) {
if (($variables[$variable->name] = unserialize($variable->value)) === FALSE) {
print $variable->name;

Once I had my variable names, I was able to find them in the database, using phpMyAdmin, and edit them. I found that I had something like s:5:" in there. This had been truncated, and should have been something like s:5:"hello". The first letter indicates that the variable is a string. The number indicated how many characters the string has, and the value of the string is encapsulated within double quotes.

Afterwards, I just changed back to the way it was before, and my unserialize problems vanished!


Brian said...

This seems like an interesting problem. Do you have any thoughts as to what caused the initial data corruption?

Bill said...

Thanks - this just made troubleshooting easier!