Reputation: 1224
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?
Upvotes: 0
Views: 9277
Reputation: 141
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!
Upvotes: 1
Reputation: 20997
You're assigning data twice.
$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.
$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.
Upvotes: 0
Reputation: 17042
$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)
Upvotes: 2