I've entered a video in the Canadian Doritos Viralocity contest. I'd appreciate if you checked it out (especially if this blog has helped you in any way). Thanks!

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.


No Comments »

No comments yet.

Leave a comment