base64_decode truncates数组导致unserialize返回false

时间:2016-06-15 10:08:16

标签: php serialization base64 decode

我正在在线应用程序上记录用户操作。所以,我有一个相当大(但不是那么大)的多维数组,我将其存储到数据库中。 为此,我使用base64编码/解码和序列化/反序列化。

以下是我用来序列化数组的方法:

//string that you looking for
    char suchString[40];
    //Variable to select what kind of data you looking for
    int selection= 0;
    //should be true if string is less than 39 signs
    bool suchStrisOk = false;
    //will be true if string is founded
    bool foundStr = false;
    cout << "weahlen die aus nach welchen Daten sie suchen wollen"<<endl;
    cout << "1.Anrede\n2.Vorname\n3.Nachname\n4.strassenname\n5.plz\n6.Wohnort\n7.vorwahl\n8Telefonnummer\n";
    //what should function look for
    cin >> selection;
    cout << "Geben sie ein welchen Begriff sie suchen möchten" << endl;
    //what string should function look for?
    cin >> suchString;
    if (cin) {
        suchStrisOk == true;
    }
    else {
        suchStrisOk == false;
    }

这是我如何取回数据:

$this->detail = base64_encode(serialize($detailedInfo));

有时候,unserialize会返回false。

我调试了一点,它接缝当数组变大时,base64编码或解码会截断数据。 这是在较小的数组上转储base64_decode的结果:

$logDetail = unserialize(base64_decode($log->detail));

这里反序列化工作正常!

当数组较大时,接缝被截断(由于主体字符数限制,只是字符串行的结尾):

a:2:{s:8:"products";a:7:{i:15;a:5:{s:9:"idProduct";s:2:"15";s:3:"zuc";s:6:"110224";s:8:"oldValue";s:6:"15,000";s:5:"value";s:2:"17";s:9:"hoppValue";d:17;}i:837;a:5:{s:9:"idProduct";s:3:"837";s:3:"zuc";s:6:"322713";s:8:"oldValue";s:6:"15,000";s:5:"value";s:2:"17";s:9:"hoppValue";d:17;}i:2405;a:5:{s:9:"idProduct";s:4:"2405";s:3:"zuc";s:6:"849755";s:8:"oldValue";s:6:"15,000";s:5:"value";s:2:"17";s:9:"hoppValue";d:17;}i:2433;a:5:{s:9:"idProduct";s:4:"2433";s:3:"zuc";s:6:"855839";s:8:"oldValue";s:6:"15,000";s:5:"value";s:2:"17";s:9:"hoppValue";d:17;}i:2494;a:5:{s:9:"idProduct";s:4:"2494";s:3:"zuc";s:6:"921545";s:8:"oldValue";s:6:"15,000";s:5:"value";s:2:"17";s:9:"hoppValue";d:17;}i:2625;a:5:{s:9:"idProduct";s:4:"2625";s:3:"zuc";s:6:"971694";s:8:"oldValue";s:6:"15,000";s:5:"value";s:2:"17";s:9:"hoppValue";d:17;}i:2626;a:5:{s:9:"idProduct";s:4:"2626";s:3:"zuc";s:6:"971769";s:8:"oldValue";s:6:"15,000";s:5:"value";s:2:"17";s:9:"hoppValue";d:17;}}s:5:"dates";a:3:{s:4:"post";a:2:{s:7:"dtStart";a:1:{i:0;s:10:"06/06/2016";}s:5:"dtEnd";a:1:{i:0;s:10:"08/08/2016";}}s:4:"hopp";a:2:{s:7:"dtStart";a:1:{i:0;s:10:"2016-06-06";}s:5:"dtEnd";a:1:{i:0;s:10:"2016-08-08";}}s:5:"weeks";a:11:{i:0;a:2:{s:5:"first";s:10:"2016-06-06";s:4:"last";s:10:"2016-06-12";}i:1;a:2:{s:5:"first";s:10:"2016-06-13";s:4:"last";s:10:"2016-06-19";}i:2;a:2:{s:5:"first";s:10:"2016-06-20";s:4:"last";s:10:"2016-06-26";}i:3;a:2:{s:5:"first";s:10:"2016-06-27";s:4:"last";s:10:"2016-06-30";}i:4;a:2:{s:5:"first";s:10:"2016-07-01";s:4:"last";s:10:"2016-07-03";}i:5;a:2:{s:5:"first";s:10:"2016-07-04";s:4:"last";s:10:"2016-07-10";}i:6;a:2:{s:5:"first";s:10:"2016-07-11";s:4:"last";s:10:"2016-07-17";}i:7;a:2:{s:5:"first";s:10:"2016-07-18";s:4:"last";s:10:"2016-07-24";}i:8;a:2:{s:5:"first";s:10:"2016-07-25";s:4:"last";s:10:"2016-07-31";}i:9;a:2:{s:5:"first";s:10:"2016-08-01";s:4:"last";s:10:"2016-08-07";}i:10;a:2:{s:5:"first";s:10:"2016-08-08";s:4:"last";s:10:"2016-08-14";}}}}

然后,unserialize返回false。

我做错了吗?是否对要编码/解码的数组的大小有限制(在任何地方都找不到任何信息)

这是logDetail数组的一个示例(一个小例子,有效):

(...) a:5:{s:9:"idProduct";s:4:"2237";s:3:"zuc";s:6:"712854";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2267;a:5:{s:9:"idProduct";s:4:"2267";s:3:"zuc";s:6:"712954";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2285;a:5:{s:9:"idProduct";s:4:"2285";s:3:"zuc";s:6:"712975";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2287;a:5:{s:9:"idProduct";s:4:"2287";s:3:"zuc";s:6:"712979";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2312;a:5:{s:9:"idProduct";s:4:"2312";s:3:"zuc";s:6:"753212";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2317;a:5:{s:9:"idProduct";s:4:"2317";s:3:"zuc";s:6:"754455";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2324;a:5:{s:9:"idProduct";s:4:"2324";s:3:"zuc";s:6:"757439";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2336;a:5:{s:9:"idProduct";s:4:"2336";s:3:"zuc";s:6:"759302";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2338;a:4:{s:9:"idProduct";s:4:"2338";s:3:"zuc";s:6:"759302";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";}i:2343;a:5:{s:9:"idProduct";s:4:"2343";s:3:"zuc";s:6:"765495";s:8:"oldValue";s:6:"12,000";s:5:"value";s:2:"15";s:9:"hoppValue";d:15;}i:2351;a:5:{s:9:"idProduct";s:4:"2351";s:3:"zuc";s:6:

1 个答案:

答案 0 :(得分:0)

看起来很严重我没有遇到那个问题

基本上我会重现您的数据并将其纳入您的序列化和base64的想法 我制作了这段代码并且有效......

<?php 

$products = [];
$dates = ['post' =>
            ['dtStart' =>
                [0 => '06/06/2016'], 
            'dtEnd' => 
                [0 => '08/08/2016']
            ]
            ,'hopp' => 
            ['dtStart' =>
                [0 => '2016-06-06'], 
            'dtEnd' => 
                [0 => '2016-08-08']
            ]
        ];

for($i = 0, $l = 11; $i < $l; $i++){
    $dates['weeks'][] = ['first' => '2016-06-06', 'last' => '2016-06-12'];
}

for($j = 0, $m = 7; $j < $m; $j++){
    $products[$j] = [
        'idProduct' => $j,
        'zuc' => random_int ( 100000 , 999999 ),
        'oldValue' => random_int ( 1 , 50 ),
        'value' => random_int ( 1 , 50 ),
        'hoppValue' => random_int ( 1 , 50 )
    ];
} 

$data = ['products' => $products, 'dates' => $dates];

ob_start();
var_dump($data);
$firstResult = ob_end_clean();

$rawData = base64_encode(serialize($data));

ob_start();
var_dump(unserialize(base64_decode($rawData)));
$secondResult = ob_end_clean();

var_dump(strcmp($firstResult, $secondResult)); 

结果是int(0) PHP.net说:Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.表示没有区别。我认为你的出局是成功的,或者数据库修剪数据,试着弄明白。