6

How come unserialize isn't restoring my array? See code below..

// prints a:1:{s:8:"txn_type";s:32:"recurring_payment_profile_cancel";}
echo $item['response']; 

// prints nothing
print_r(unserialize($item['response']));

I understand why the print_r($response) gives me nothing

** edit - I noticed this

Notice: unserialize() [function.unserialize]: Error at offset 6 of 2797 bytes in /home/reitinve/public_html/action/doc.php on line 13

What does that mean?

0

4 Answers 4

3

Is it possible $item['response'] contains some whitespace before or after it?

Check strlen($item['response']) gives you 61.

Edit: It seems to work with whitespace at the end, but whitespace at the start will make it fail to unserialize.

Edit: that error message means either you have a LOT of whitespace (almost 2kb of it), or $item['response'] is being changed between the echo and the unserialize

Sign up to request clarification or add additional context in comments.

2 Comments

i tried putting a trim like unserialize(trim($item['response'])). The trim should have removed white spaces right?
Yes. The string you say is echo'd is 61 bytes, but PHP says you're trying to unserialize 2797 bytes, so something is going on...
1

works for me just fine. are you sure that $item['response'] is a string? yeah, seems like leading whitespaces.

and on your dev server php never should give you 'nothing'. it should be configured to produce all errors, warnings and notices. also you can use http://php.net/var_dump instead of print_r as it give you more information.

1 Comment

If unserialize's parameter is not a string, it will show a warning.
0

Here's why I had this problem and how I worked it out:

I was storing an array in my input similar to this:

value="<?php echo htmlspecialchars(serialize(array($a, $b))); ?>"

Here, I had to use htmlspecialchars() because of possible parse errors.

Then when I tried to unserialize, it gave me this Error at offset X of Y bytes in ... error. I printed the unserialized string to screen, realized that html equivalents of some characters was causing the error.

To be more clear, double quotes html codes %22 was causing this error. So I replaced them back with quote chars and it worked.

unserialize(str_replace('%22', '"', $_POST['serialized']));

So it's better to check if there's any html codes in the serialized string and replace them back with original characters.

Comments

0

Also be careful if you ever try to put a serialized array in a textarea, to eventually pass it somewhere else via Ajax, you may encouter problems with special characters like the Ampersand (&) that will be convertsed to "&amp;", and this is enough for your "serialized" array not to restore.

I found the use of rawurlencode and rawurldecode very helpful to make my serialization bulletproof, no matter how it is carried across my scripts;

$myArray = array("Bugs Bunny", "Tom & Jerry");

$serialized = rawurlencode(serialize($myArray));

$myUnserializedArray = rawurldecode(unserialize($serialized));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.