PHP unserialize() Error at Offset [duplicate] - php

This question already has answers here:
How to repair a serialized string which has been corrupted by an incorrect byte count length?
(17 answers)
Closed 9 years ago.
The exact error I am getting is:
ErrorException [ Notice ]: unserialize(): Error at offset 5 of 59 bytes
The serialized data returned from a TEXT field in MySQL is (Encoding: utf8, Engine: InnoDB):
a:1:{s:12:"display_name";s:6:"Foo";}
After unserializing the output is:
array(1) { [0]=> bool(false) }
I am expecting something like this:
array(1) { ["display_name"]=> string(6) "Foo" }
I am using FuelPHP 1.6.1, PHP 5.4.10, MySQL 5.5.29, InnoDB 1.1.8 on Mac OS 10.8.4. Some of the serialized entries work on unserialize while others don't, if I copy one that works and paste into one that does not it shows the same error. I believe its a character issue and I have tried to encode into utf8, urlencode, and stripslashes but nothing seems to work.
Any help is appreciated!
Update
The serialized string had a type, I did this when pasting it here for privacy to the actual display name. Here is the actual string:
a:1:{s:12:"display_name";s:3:"Foo";}
Thanks to #robw for pointing this out.
Update & Answer
I am running the serialized data through html_entity_decode() now as such:
$unserialized = unserialize(html_entity_decode($serialized, ENT_QUOTES));
Thank you for the help and advice!

Your problem is here: s:6:"Foo"
Foo is not 6 characters long... so that will never parse as expected.
Try: s:3:"Foo"
PS: This looks like you edited data in MySQL, then when your application tried to parse it, it errored out. You should never edit a serialized value in the database unless you're 100% sure you've modified the LENGTH in accordance to the TYPE and VALUE.

Related

unserializing data array with php from mysql database

I have the following serialized PHP array stored in a mySQL database:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}
Now, I managed to output that value with:
$my_data=mysql_result($result,$i,"my_data");
echo "$my_data";
but I can't manage to unserialize it. I tried this but it doesn't work:
$my_data=unserialize($my_data);
When I add that in between, all I get is a blank page. Any ideas?
Maybe you should look at the process of inserting the the value into the database. Is it possible that after the values are serialized, that they were encoded in someway, such as to html entities or something?
I ran a test locally and I got the same error message. Here is output:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";} Notice: unserialize(): Error at offset 12 of 62 bytes in /srv/localhost/public_html/test.php on line 6
Here is code
<?php
$value = htmlentities('a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}');
echo $value;
unserialize($value);

Can't unserialize a string in php

Yes, i know. I see a lot of questions about it.
But no one works for me until now.
I have a blog in wordpress who use serialized data for store some custom fields.
It works great, but when i moved all the blog to another folder, all serialized data gonne from wordpress (but it stills in the database)
So, wordpress don't detect it.
Now... i'm figthing with the code to know why isn't working.
At the end... i just thinked, well, i gonna do a code for get the serialized data and it will work.
Now i'm lost, i have this:
$data = 'a:7:{s:4:"zoom";s:2:"18";s:8:"latitude";s:8:"41.37233";s:9:"longitude";s:7:"1.04283";s:11:"address_one";s:16:"Finca Riudabella";s:11:"address_two";s:33:" s/n - 43430 Vimbodí (Tarragona)";s:3:"pin";s:77:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png";s:6:"bg_img";s:0:"";}';
$data = "a:7:{s:4:1}";
$data = trim($data);
var_dump($data);
var_dump(unserialize($data));
I tried with an original serialized string from the database (the fisrt line) and returns false and a error.
I done one a little bit simple, and say the same.
My error is:
Notice: unserialize() [function.unserialize]: Error at offset 5 of 11 bytes in C:\xampp\htdocs\unser.php on line 6
bool(false)
So, i don't know why i can't get data from the string!
I tried this tool http://unserialize.net/serialize and my data work just as expected :\ i need to do something else?
$broken_data = 'a:7:{s:4:"zoom";s:2:"18";s:8:"latitude";s:8:"41.37233";s:9:"longitude";s:7:"1.04283";s:11:"address_one";s:16:"Finca Riudabella";s:11:"address_two";s:33:" s/n - 43430 Vimbodí (Tarragona)";s:3:"pin";s:77:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png";s:6:"bg_img";s:0:"";}';
$data = serialize(
array(
"zoom" => "18",
"latitude" => "41.37233",
"longitude" => "1.04283",
"address_one"=>"Finca Riudabella",
"address_two"=>" s/n - 43430 Vimbodí (Tarragona)",
"pin"=>"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png",
"bg_img"=> ""
)
);
// The right data
var_dump($data);
// Your data.
var_dump($broken_data);
var_dump(unserialize($data));
As you can see the serialization of $data is correct.
The serialized $broken_data seems to have incorrect string length at "pin".
In $broken_data it's stated to be 77 characters (s:77) but in reality it's 67 characters long (s:67)
I ran into this exact same problem recently and spent countless hours finding a way to restore the bad data. I walked through every function and line of code in the Spyropress theme, until it led me to page after page of functions inside WordPress, and finally leading me to understand that the maybe_unserialize() was failing.
That led me on a quest to figure out why, where I stumbled across a few threads like this one, that pointed out how the string counts could be off. It turns out that a find/replace had been performed on the data throughout the entire database, ruining tons of pages built with a theme.
In my case, I had to automate a "fix", and I posted my code in another thread. I'm not sure if better to post here or just link there, so I'm linking. You can see my code to "fix" serialized data here:
Handy code to automatically fix broken serialized data string counts.
My solution works with large datasets, containing HTML/CSS, escaped double quotes, newlines, and tons of special characters. I thought it might help those who find this page before the other one (like I did).
Cheers!
You're assigning data twice.
First assignment
$data = 'a:7:{s:4:"zoom";s:2:"18";s:8:"latitude";s:8:"41.37233";s:9:"longitude";s:7:"1.04283";s:11:"address_one";s:16:"Finca Riudabella";s:11:"address_two";s:33:" s/n - 43430 Vimbodí (Tarragona)";s:3:"pin";s:67:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png";s:6:"bg_img";s:0:"";}';
It fails on this: s:77:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png"; but as you've may noticed length of provided url is 67, when you manually change it to 67, and use: var_dump( unserialize( $data)); You'll get this:
array(7) {
["zoom"]=>
string(2) "18"
["latitude"]=>
string(8) "41.37233"
["longitude"]=>
string(7) "1.04283"
["address_one"]=>
string(16) "Finca Riudabella"
["address_two"]=>
string(33) " s/n - 43430 Vimbodí (Tarragona)"
["pin"]=>
string(67) "http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png"
["bg_img"]=>
string(0) ""
}
How it got changed? I don't know, there doesn't seem to be special characters, maybe class implementing Serializable provides wrong string length for URL.
Second assignment
$data = "a:7:{s:4:1}";
This is just wrong, declaring array with 7 items, providing just one... And that one is string that is supposed to have length 4 ("str1") and instead providing just 1... This just shouldn't and cannot work.

How do I get PHP to accept ISO-8859-1 characters in general?

This has been bugging me for ages and I want to get to the bottom of this once and for all. I have an associative array which fields I have defined using ISO-8859-1 characters. For instance:
array("utført" => "red");
I also have another array that I have loaded in from a file. I have printed this array out in a browser, checking that values like Æ, Ø and Å is intact. I try to compare two fields from these arrays and I'm slapped by the message:
Undefined index: utfã¸rt on line 39
I can't help but sob. Every single damn time I involve any letters outside UTF-8 in a script they are at some point converted into ã¸r or similar nonsense.
My script file is encoded in ISO-8859-1, the document from which I'm loading my data is the same, and so is the MySQL table I'm trying to save the data to.
So the only conclusion I can draw is that PHP isn't accepting just any character-sets into it's code, and I have to somehow force PHP to speak Norwegian.
Thanks for any suggestions
Just FYI, I won't accept any answers in the lines of "Just don't use those characters" or "Just replace those characters with UTF equivalents at file load" or any other hack solutions
When you read your data from external file try to convert them in proper encoding.
Something like this I have on my mind...
$f = file_get_contents('externaldata.txt');
$f = mb_convert_encoding($f, 'iso-8859-1');
// from this point deal with $f whatever you want
Also, look at mb_convert_encoding() manual for more info.

serialization and utf-8 in PHP

Hi All
I'm trying to serialize array that contains some utf-8 code:
....["value"]=> string(13) "مغادرة1"....
but after serializing the array,it look like this:
value";s:13:"??????
I think that the error that i get:
Message: unserialize() [function.unserialize]: Error at offset 685 of 701 bytes
is related to wrong serialization to utf-8 code
So How to serialize array that contains utf-8 code?
Thank You
Thank's for all replies
The problem was in storing the data in the database,and not in serialization,the type of field that i stored the ser content was latin,i change it to utf-8 and everything works fine

PHP unserialize error at offset, works on some servers, not others

I have code that works on a handful of servers, but not others which is coming up with serialised data. I call a page like this:
http://domain/index.php/sales/Drilldowns?params=a:12:{s:13:"selectionType";s:8:"facility";s:8:"dateType";s:5:"daily";s:10:"dateOption";s:9:"drilldown";s:6:"metric";s:13:"bookingAmount";s:9:"companyFK";s:2:"11";s:10:"facilityFK";s:0:"";s:7:"classFK";s:0:"";s:15:"customDateStart";s:4:"null";s:7:"newDate";s:10:"2010-11-01";s:10:"metricName";s:10:"Bookings%20$";s:16:"currentDateRange";s:10:"11/01/2010";s:23:"currentMetricNavigation";s:8:"DELDELTE";}&getExcel=0
This is the code I'm using:
protected function getRequestVariables(){
if(isset($_REQUEST['params'])){
var_dump($_REQUEST['params']);
echo 'length:'.strlen($_REQUEST['params']);
$vars = unserialize($_REQUEST['params']);
var_dump($vars);
} else {
$vars = $_REQUEST;
// unset saved drilldown options
$this->ci->session->svar_set('postVars', null);
}
This is a var_dump output:
string(447) "a:12:{s:13:\"selectionType\";s:8:\"facility\";s:8:\"dateType\";s:5:\"daily\";s:10:\"dateOption\";s:9:\"drilldown\";s:6:\"metric\";s:13:\"bookingAmount\";s:9:\"companyFK\";s:2:\"11\";s:10:\"facilityFK\";s:0:\"\";s:7:\"classFK\";s:0:\"\";s:15:\"customDateStart\";s:4:\"null\";s:7:\"newDate\";s:10:\"2010-11-01\";s:10:\"metricName\";s:10:\"Bookings $\";s:16:\"currentDateRange\";s:10:\"11/01/2010\";s:23:\"currentMetricNavigation\";s:8:\"DELDELTE\";}"
When that gets processed I get the following error:
A PHP Error was encountered
Severity: Notice
Message: unserialize() [function.unserialize]: Error at offset 6 of 447 bytes
Filename: plugins/Drilldowns.php
Line Number: 93
I'm trying this on 5.2.13 - works on some Linux, some OS X, not others. Have checked php.ini, charset (I think) - I can't figure it out for the life of me. I've tried things as simple as
string(18) "a:1:{s:3:\"sam\";}" length:18
and it still errors. Any clue as to why?
It's the backslashes in front of the quotes: \"
When you remove them, it works.
var_dump(unserialize('a:12:{s:13:"selectionType";s:8:"facility";s:8:"dateType";s:5:"daily";s:10:"dateOption";s:9:"drilldown";s:6:"metric";s:13:"bookingAmount";s:9:"companyFK";s:2:"11";s:10:"facilityFK";s:0:"";s:7:"classFK";s:0:"";s:15:"customDateStart";s:4:"null";s:7:"newDate";s:10:"2010-11-01";s:10:"metricName";s:10:"Bookings $";s:16:"currentDateRange";s:10:"11/01/2010";s:23:"currentMetricNavigation";s:8:"DELDELTE";}"'));
The servers this works on, probably have magic quotes turned on.
I had this problem and it took me a while to solve it. I just couldn't find any good solution but this is what I did to solve my situation:
base64_encode(serialize($User)); // make sure to encode the serialized object
unserialize(base64_decode($User)); // decode it before unserializing

Categories