PHP: unserialize() Error at offset… (simple solution)

Here’s what I was trying to do:

  • serialize an array on one page (PHP: serialize($array))
  • print out the results using echo or print_r
  • grab the serialized data from another script using file_get_contents
  • unserialize that array so I could go about my business

but I kept getting this error:

unserialize() [function.unserialize]: Error at offset 0 of 3457 bytes

I looked around and couldn’t find a simple a solution. I’m sure this error can occur in other ways, but I was trying with the simplest of arrays:

Array("Ryan" => "Bosinger");

My problem was that my echo statement was throwing some whitespace in there. That’s all. This fixed it:

$data["search_results"] = file_get_contents("http://localhost/sphider/search.php?query=" . $search_for . "&search=1&start=" . $start);
$result_string = trim($data["search_results"]);
$result_array = unserialize($result_string);

Basically, try trimming the whitespace before unserializing. Hope that helps!

Also, I was using Codeigniter but I really don’t think that has anything to do with it.


11 Comments »

  1. Thanks for the fix, worked out great for me. Kept getting that pesky “Error at offset” error.

    I have been programming with PHP for at least 5 years and I have never come across this error until now. You mentioned you were using Codeigniter because this is the first time I have run into the error and I was using Expression Engine which is based on Codeigniter.

    Again thanks for the tip. Using trim() worked great.

    Comment by Joshua Powell — April 26, 2010 @ 9:06 pm

  2. I just want to say Thanks,
    The trim() solution did not work in my case, but your advice put me on the path to what I believe is at the heart of this problem- database field lenght. As it turns out, serialize() will include a few control characters with the data. If the field lenght is too small, those control characters will be truncated with no error from the system that they were not included with your data. As it is, the bug dont show up until you are trying to retrieve the data with the “unserialize()” function. Of course, with some of the control characters missing, the retrieval fails. This also explains, why trim() will work in some cases, depending on how much white space is can be removed with the trim(). For a permanent fix, increase the feild lenght in the database by a few characters to include the serialize control characters especially so if the data to be stored will vary in size.

    Comment by Melli — May 17, 2010 @ 5:06 am

  3. another case: check your database field encode

    Comment by Minh Tuan — December 6, 2010 @ 4:23 am

  4. Sorry. my mean database field charset. utf8_general_ci for example

    Comment by Minh Tuan — December 6, 2010 @ 4:26 am

  5. Well, I was using curl to get the echoed data.
    Worked fine till I changed all the encodings of all pages to UTF-8. I think I might have missed some, not sure.
    But then I later tried and there it was, the same error.
    I fixed it by echoing a base64 encoded string, and then decoding it after. Trim() didn’t work for me however, so I thought I should post this for others.

    Comment by frostymarvelous — December 9, 2010 @ 2:28 pm

  6. I figured out what was causing the error so I figured I’d post it just in case.
    You were right, there were some bytes after all. Seems the editor I was using threw in a few bytes at the beginning of the document when I saved as UTF-8. This is known as BOM (byte order marker). Luckily, it has an option to save UTF-8 without the BOM. That solved the issue.
    Seems notepad does this also but doesn’t have the option to save without BOM. The editor I used is notepad++. One of the best freeware I should add.
    On a related issue, this also causes the session_start() headers cannot already sent error. That’s how I actually figured it out.
    Hope this helps.

    Comment by frostymarvelous — December 11, 2010 @ 6:25 am

  7. It seems the problem is in fact something to do with CodeIgniter, I’ve been using serialize()/unserialize() for a few years now and never had this problem until I recently started using CI. Thanks for the fix :)

    Comment by Damo — January 15, 2011 @ 10:29 pm

  8. I was getting the same error while using CodeIgniter. Adding trim() before trying to unrealized worked beautifully. Not sure if it matters, but I didn’t get this until migrating from Ubuntu 8.04 to Ubuntu 10.04.

    Thank you for the awesome time saving tip!

    Aloha – Josh

    Comment by Josh Sommers — July 18, 2011 @ 11:24 pm

  9. [...] others prefer to use [...]

    Pingback by unserialize() Error at Offset… Different solutions | WPhub.biz — December 14, 2011 @ 3:19 pm

  10. [...] others prefer to use [...]

    Pingback by wp-coder.net » unserialize() Error at Offset… Different solutions — December 14, 2011 @ 11:06 pm

  11. [...] others prefer to use [...]

    Pingback by A Free wordpress newsletter » unserialize() Error at Offset… Different solutions — December 14, 2011 @ 11:10 pm

Leave a comment