【问题标题】:How come when I try to unserialize something the result is just empty?当我尝试反序列化某些结果时,为什么结果只是空的?
【发布时间】:2013-06-05 05:26:52
【问题描述】:

例如,我有字符串

a:1:{i:0;a:3:{s:4:"name";s:56:"Song → Black Meen : “So Clean (feat. Dullaah Jin)”";s:4:"file";s:98:"http://localhost/peace.worldwide/wp-content/uploads/2013/05/03-So-Clean-feat.-Dullaah-Jin.mp3";s:9:"condition";s:3:"all";}}

我想反序列化它。

当我将它传递给 unserialize() 时,结果什么都没有(例如,print_r(unserialize(/*I put the string here*/)) 完全没有打印)。

为什么会这样?

【问题讨论】:

  • 顺便说一句,我很好奇,你用那个序列化做什么?
  • 我正在将它从数据库中提取出来。我没有制作原始应用程序,但我正在使用它放入数据库中的数据。
  • 我会改用JSON。有点更可靠,因为您可以使用 PHP 以外的其他语言将数据解析为对象/数组。
  • 这是真的,我完全愿意,但我没有制作原始应用程序,所以我必须弄清楚如何修改所有这些。我以为我可以反序列化它,但我想不是。该字符串(具有不正确的长度字段)是应用程序正在使用的字符串,并且该应用程序可以正常工作。我必须深入挖掘才能了解原因。

标签: php serialization deserialization


【解决方案1】:

在 error_reporting 中打开您的通知:

PHP Notice:  unserialize(): Error at offset 204 of 229 bytes in php shell code on line 1

围绕这些偏移量,您有一个

s:98:"http://localhost/peace.worldwide/wp-content/uploads/2013/05/03-So-Clean-feat.-Dullaah-Jin.mp3"

表示应该有一个长度为98的字符串,但字符串只有93个字节长。

【讨论】:

  • 我解决了我的问题。一开始没有想到。但就我而言,数字匹配。
  • @trusktr var_dump(unserialize('a:1:{i:0;a:3:{s:4:"name";s:4:"blah";s:4:"file";s:25:"http://localhost/blah.mp3";s:9:"condition";s:3:"all";}}'));array(1) { [0]=> array(3) { ["name"]=> string(4) "blah" ["file"]=> string(25) "http://localhost/blah.mp3" ["condition"]=> string(3) "all" } }
  • 嗯..我的显示bool(false)...我只会发布实际的东西。我认为它是无害的。
  • @trusktr 不。你的长度又错了:你在 url 之前有 s:98,但这与字符串长度 (93) 不匹配。
  • Hmmm.... 但这是未从数据库修改的实际字符串。在我可以反序列化它之前是否需要对其进行某种类型的操作? (使用它的应用似乎运行良好)。
【解决方案2】:

我发现了导致数据库中的序列化字符串混乱的问题。我已经搜索/替换了数据,以将 str8wayent.com 替换为 localhost,这显然会更改序列化值,但不会更改数据库中的长度字段。

我发现这个很好的脚本来固定 sql 转储文件中的序列化长度字段: github.com/Blogestudio/Fix-Serialization

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 2022-01-07
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多