Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

PHP Serialize() & Unserialize() Issues

24 Responses »

I've been working on some very large forms lately and I've come to the conclusion that creating a database scheme around them wouldn't be the best option because:

  1. My customers don't need to analyze all form submissions as a whole -- form information is simply used on a per-submission basis (like a job application, for example).
  2. Making updates to these forms would be very costly since it would take quite a bit of time to add and remove DB fields as well as update the HTML form.
  3. I'd like to revert the information into an array format just like it came in easily.

For that reason, I've been using the serialize() and unserialize() functions often. Serializing an array keeps the information in an array format, so to speak, but in one long string. Anyways, I ran into the following error when testing unserialize on some information that I had serialized:

Notice: unserialize(): Error at offset 2 of 52 bytes in file.php on line 130

It turns out that if there's a ", ', :, or ; in any of the array values the serialization gets corrupted. I've found the following fix for this issue on PHP.net:

//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));

//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));

It's a great fix to simple problem!

Discussion

  1. May 16, 2008 @ 11:41 am

    Great fix indeed!!! Smart thinking!

  2. August 6, 2008 @ 1:50 pm

    Works like a charm. I haven’t found any issue with this work-around. You’re one smart duck.

  3. December 16, 2008 @ 10:13 am

    Great “fix”. :-) You also may want to look into a document oriented store (e.g. CouchDB) vs. serializing data into a RDBMS. This would allow you to stay flexible when storing data, but also for later querying/analyzation.

  4. March 26, 2009 @ 11:46 pm

    Thanks for this tip! I almost deleted all my new code related to serialization when I decided to search for solutions and found this trick.

    It’s not very clear for me how it works if it encodes string after it’s serialized and decodes before it’s unserialized, but for as long as it works I’m happy!

    Thanks again!

  5. shaddi
    June 2, 2009 @ 9:38 am

    @Shimon, it’s not actually encoding the string, it’s encoding the serialization. If you encode the serialized array, you’ll have to decode the array before you unserialize it.

    Great fix!

  6. June 2, 2009 @ 11:07 am

    @Shaddi
    My accent was made on why it does encoding after all, when serialization is done already. I understand that if you encode string after serialization you’d have to decode before unserialization :)

  7. August 12, 2009 @ 4:36 am

    Thank you, I was having an issue with serialize() and unserialize() and this post solved it :)

  8. paul
    September 13, 2009 @ 7:38 pm

    Thanks for this solution.

    Serialize and unserialize is a godsend in lieu of sessions and forms, but could be better constructed.

    Excellent advice.

  9. September 24, 2009 @ 9:42 am

    Great blog you’re writing! I think the easiest way to prevent these issues is not to use serialize() function. Why not use the implode() function, it can do the same as far as i know!

  10. txyoji
    October 1, 2009 @ 2:25 pm

    When storing very long strings in MySQL, make sure you check the length.
    MySQL will silently truncate data longer than the allotted field.

    Also, using this technique instead of storing in a database means ‘schema’ changes must be done with a php script vs using a db script to migrate data.

  11. December 10, 2009 @ 4:05 am

    It works for me, but you must make sure your table field has enough varchar space eg: varchar(100) as the string is too long, and you must know how to display arrays.

  12. vishal
    January 4, 2010 @ 2:37 am

    Thanks mate that was Gr8 Help

  13. john
    February 2, 2010 @ 3:14 pm

    thank you so much – i am not sure why this worked and my other code didnt, but FABULOUS!

    thanks

  14. walid
    February 8, 2010 @ 10:40 pm

    Thanks a lot different servers work differently, the same code wouldnt work on our new server until i implemented this

    kudos to putting this up

  15. steve
    February 10, 2010 @ 3:08 pm

    The real question is why PHP doesn’t incorporate this into serialize() to begin with.

  16. nick
    February 19, 2010 @ 6:09 pm

    @Peter:

    serialise can handle objects too. This isn’t true of implode().

  17. dave
    April 2, 2010 @ 8:04 am

    I understand what Shimon is saying and i agree.

    I see that if you are moving the serialized value in and out of a database it should be encoded as base64, but the issue is unserializing a serialized array that contains a ‘ (in my case) – encoding and then decoding does not solve that issue as you get the same string back!

  18. devdutt
    May 22, 2010 @ 5:03 am

    Thanks Mr. David Walsh this solved my big time problem.

  19. praveen
    June 8, 2010 @ 8:43 am

    @Devdutt: This solution not solved my problem, i am serializing large data and storing into database, when unserialized not working. Using php 5.0.1. it is giving offset error. “Notice: unserialize() [function.unserialize]: Error at offset 45393 of 65533 bytes “.

    Thanks in advance

  20. June 17, 2010 @ 4:51 am

    @Praveen.. It could be your database field has few character space, and it store only some few serialized information. This must bring some problem during unserialize(). Try to make enough space in your field, let say varchar(2000) and see..

  21. harinder
    July 8, 2010 @ 2:59 am

    Ita a great fix. But there is a problem when you save a field having space in its naming.

  22. jankes
    July 8, 2010 @ 4:03 am

    @Gregory Mlay:
    best way to store this would be BLOB column, ex. if you run into character set problem your serialization will remain intact, else your data can get corrupted.

  23. August 14, 2010 @ 11:08 pm

    Thanks for the fix. This is what I am looking for. Good to serialize data when storing into cookies.

  24. tom
    August 23, 2010 @ 11:49 am

    You need to escape a serialized string in a manner appropriate for your DB, just as you do for any string. For example, mysql_real_escape_string() or prepared statements in the case of MySQL.

    If you base64_encode() the serialized string then you will probably obviate escaping regardless of database it since the base64 code table uses only ASCII’s alpha, numeric, + and / characters. But that doesn’t mean it’s a good solution.

    I think base64_encode() not a good replacement for using your DB’s correct escape procedures for efficiency reasons. Sometimes code fragments found on the web will work as drop-in but are a poor substitute for understanding.

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!