0

I try to serialize object data to be saved into a file by php using serialize package by PHP.js.

Chrome: fine FF: fine IE9: fine IE9 in compatibility mode (essentially IE7): not fine.

Error from console:

SCRIPT5022: Exception thrown and not caught 
serialize.min.js, line 144 character 55

Serialized the data basically looks like this:

a:180:{s:40:"Aleksis Kiven tie 15<br>04200 Kerava<br>";a:2:{i:0;d:60.4012598;i:1;d:25.09659910000005;}

Unserialized:

{"Aleksis Kiven tie 15<br>04200 Kerava<br>": [60.4012598, 25.09659910000005]}

Javascript that handles the caching looks like this:

function saveCache(data) {
    sdata = serialize(data);
    $.ajax({
        type: 'POST',
        url: 'http://localhost/foobar/files/coordinates.php',
        data: {
            'do': 'write',
            'data': sdata
        }
    });
}

I'm caching geocoding results by address into a text file.

Please ask more if I'm not making sense.

MAJOR EDIT: I corrected the examples as pointed out. Also the major thing is that the issue really changed; it isn't actually an issue with serialize(), but with unserialize().

Simon Josef Kok
  • 745
  • 1
  • 8
  • 22
  • Do you have an example that actually works in PHP? Your example of serialized data does not work with unserialize() in PHP itself and your unserialized example is not valid JavaScript, so I can't really test it. There was a commit two years ago or so to fix an IE problem, so also make sure you are using the latest code. – Brett Zamir Sep 06 '12 at 02:25
  • I'm sorry, but I don't know how it would help. My serialized and unserialized examples not being valid is not really the issue. The point is that everything works, except in IE7, which means that there is a bug or a no-feature in IE7 that either can or cannot be circumvented. I was hoping that somebody could tell me if what I try to do is doable in the first place, in IE7 that is. In other browsers it's working fine. – Simon Josef Kok Sep 06 '12 at 04:22
  • It would help because we can actually confirm and debug it ourselves. – Brett Zamir Sep 06 '12 at 10:18

1 Answers1

0

I figured it out myself after all.

When I took a closer look it wasn't serialize() after all that caused the error but rather unserialize() which is in the same package with me.

At some point the cache went corrupt and for some reason IE9 in comp-mode was the only browser of those I tested that didn't tolerate it. The reason for the cache corrupting was stacking utf8_encodes/decodes that started messing things up.

It went like this:

  • Cache is built and serialized using serialize(). All Ok.
  • PHP checks if the data is really UTF-8 and performs an utf8_encode accordingly. Saves data. All Ok.
  • Next page load. PHP returns stored data using AJAX. An UTF-8 header is sent just in case. data is unserialize()d and dealt with accordingly. All Ok.
  • Cache is possibly updatet with new content and serialize()d. Alerting the serialized data yields a surprise: chinese characters and corruption in place of åäö!

The solution lies in unserialize() performing an unnecessary utf8_decode(). The page is UTF-8. All my javascript files are UTF-8. The cached data is made many times sure to be UTF-8. So there souldn't be and isn't any reason to decode UTF-8. I'm not a 100% sure how exactly this crapped out the data, but commenting out the utf8_decode solved the issue.

I'm a little bit disappointed in the error tolerance of modern browsers. It is too high. But well, good thing I figured it out.

Simon Josef Kok
  • 745
  • 1
  • 8
  • 22
  • Yes, you are correct. This had been removed earlier in source code (see https://raw.github.com/kvz/phpjs/master/functions/var/unserialize.js ), but the site's cache still hasn't refreshed and has the old code. – Brett Zamir Sep 06 '12 at 10:20
  • Something else must be outdated too then, because both serialize() and unserialize() list either utf8_encode() or utf8_decode() as a dependency. – Simon Josef Kok Sep 07 '12 at 04:37