PHP 在需要数组元素时反序列化 returns false
PHP unserializing returns false when array elements are expected
数据库中的某些行 table 在列中包含序列化数据:
select * from phpbb_config where config_name like 'convert%';
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+------------+
| config_name | config_value | is_dynamic |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+------------+
| convert_db_server | a:4:{s:4:\"dbms\";s:22:\"phpbb\db\driver\mysqli\";s:6:\"dbhost\";s:9:\"127.0.0.1\";s:6:\"dbport\";s:0:\"\";s:6:\"dbname\";s:10:\"klein-putz\";} | 1 |
| convert_db_user | a:2:{s:6:\"dbuser\";s:4:\"root\";s:8:\"dbpasswd\";s:0:\"\";} | 1 |
| convert_options | a:2:{s:10:\"forum_path\";s:16:\"../../klein-putz\";s:7:\"refresh\";i:0;} | 1 |
| convert_progress | a:3:{s:4:\"step\";s:0:\"\";s:12:\"table_prefix\";s:6:\"phpbb_\";s:3:\"tag\";s:7:\"phpbb20\";} | 1 |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+------------+
虽然未序列化时,所有 return 都为 false,而不是设置多个数组元素,这表明它们不是正确序列化的值。
可以在这里看到代码行:
听说连载的时候基本都是JSON。我通过 JSON 验证器 运行 这些,但它们都失败了。只是不清楚为什么。
您的数据库字符串中有多余的反斜杠。
试试这个代码:
<?php
$data1 = 'a:3:{s:4:"step";s:0:"";s:12:"table_prefix";s:6:"phpbb_";s:3:"tag";s:7:"phpbb20";}';
$data2 = 'a:3:{s:4:\"step\";s:0:\"\";s:12:\"table_prefix\";s:6:\"phpbb_\";s:3:\"tag\";s:7:\"phpbb20\";}';
$test1 = unserialize($data1);
$test2 = unserialize($data2);
echo "<br>case1:";
var_dump($test1);
echo "<br>case2:";
var_dump($test2);
?>
如您所见,$data1 反序列化正确,但 $data2 returns 错误。
数据库中的某些行 table 在列中包含序列化数据:
select * from phpbb_config where config_name like 'convert%';
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+------------+
| config_name | config_value | is_dynamic |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+------------+
| convert_db_server | a:4:{s:4:\"dbms\";s:22:\"phpbb\db\driver\mysqli\";s:6:\"dbhost\";s:9:\"127.0.0.1\";s:6:\"dbport\";s:0:\"\";s:6:\"dbname\";s:10:\"klein-putz\";} | 1 |
| convert_db_user | a:2:{s:6:\"dbuser\";s:4:\"root\";s:8:\"dbpasswd\";s:0:\"\";} | 1 |
| convert_options | a:2:{s:10:\"forum_path\";s:16:\"../../klein-putz\";s:7:\"refresh\";i:0;} | 1 |
| convert_progress | a:3:{s:4:\"step\";s:0:\"\";s:12:\"table_prefix\";s:6:\"phpbb_\";s:3:\"tag\";s:7:\"phpbb20\";} | 1 |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+------------+
虽然未序列化时,所有 return 都为 false,而不是设置多个数组元素,这表明它们不是正确序列化的值。
可以在这里看到代码行:
听说连载的时候基本都是JSON。我通过 JSON 验证器 运行 这些,但它们都失败了。只是不清楚为什么。
您的数据库字符串中有多余的反斜杠。
试试这个代码:
<?php
$data1 = 'a:3:{s:4:"step";s:0:"";s:12:"table_prefix";s:6:"phpbb_";s:3:"tag";s:7:"phpbb20";}';
$data2 = 'a:3:{s:4:\"step\";s:0:\"\";s:12:\"table_prefix\";s:6:\"phpbb_\";s:3:\"tag\";s:7:\"phpbb20\";}';
$test1 = unserialize($data1);
$test2 = unserialize($data2);
echo "<br>case1:";
var_dump($test1);
echo "<br>case2:";
var_dump($test2);
?>
如您所见,$data1 反序列化正确,但 $data2 returns 错误。