开发者社区> 问答> 正文

如何修复已被错误的字节数长度损坏的序列化字符串?

我正在将Hotaru CMS与Image Upload插件一起使用,如果尝试将图像附加到帖子上,则会收到此错误,否则没有错误:

unserialize()[function.unserialize]:偏移量错误

令人反感的代码(错误指向**):

/** * Retrieve submission step data * * @param $key - empty when setting * @return bool */ public function loadSubmitData($h, $key = '') { // delete everything in this table older than 30 minutes: $this->deleteTempData($h->db);

    if (!$key) { return false; }

    $cleanKey = preg_replace('/[^a-z0-9]+/','',$key);
    if (strcmp($key,$cleanKey) != 0) {
        return false;
    } else {
        $sql = "SELECT tempdata_value FROM " . TABLE_TEMPDATA . " WHERE tempdata_key = %s ORDER BY tempdata_updatedts DESC LIMIT 1";
        $submitted_data = $h->db->get_var($h->db->prepare($sql, $key));
        **if ($submitted_data) { return unserialize($submitted_data); } else { return false; }** 
    }
}

表中的数据,请注意末尾有图像信息,我不是PHP专家,所以我想知道你们/ gals会怎么想?

tempdata_value:

a:10:{s:16:"submit_editorial";b:0;s:15:"submit_orig_url";s:13:"www.bbc.co.uk";s:12:"submit_title";s:14:"No title found";s:14:"submit_content";s:12:"dnfsdkfjdfdf";s:15:"submit_category";i:2;s:11:"submit_tags";s:3:"bbc";s:9:"submit_id";b:0;s:16:"submit_subscribe";i:0;s:15:"submit_comments";s:4:"open";s:5:"image";s:19:"C:fakepath100.jpg";} 编辑:我想我已经找到了序列化位...

/** * Save submission step data * * @return bool */ public function saveSubmitData($h) { // delete everything in this table older than 30 minutes: $this->deleteTempData($h->db);

    $sid = preg_replace('/[^a-z0-9]+/i', '', session_id());
    $key = md5(microtime() . $sid . rand());
    $sql = "INSERT INTO " . TABLE_TEMPDATA . " (tempdata_key, tempdata_value, tempdata_updateby) VALUES (%s,%s, %d)";
    $h->db->query($h->db->prepare($sql, $key, serialize($h->vars['submitted_data']), $h->currentUser->id));
    return $key;
}

展开
收起
保持可爱mmm 2020-05-10 19:47:46 930 0
1 条回答
写回答
取消 提交回答
  • unserialize() [function.unserialize]: Error at offset是invalid serialization data由于长度无效引起的

    快速解决

    您可以做的是recalculating the length序列化数组中的元素

    您当前的序列化数据

    $data = 'a:10:{s:16:"submit_editorial";b:0;s:15:"submit_orig_url";s:13:"www.bbc.co.uk";s:12:"submit_title";s:14:"No title found";s:14:"submit_content";s:12:"dnfsdkfjdfdf";s:15:"submit_category";i:2;s:11:"submit_tags";s:3:"bbc";s:9:"submit_id";b:0;s:16:"submit_subscribe";i:0;s:15:"submit_comments";s:4:"open";s:5:"image";s:19:"C:fakepath100.jpg";}'; 无需重新计算的示例

    var_dump(unserialize($data)); 输出量

    Notice: unserialize() [function.unserialize]: Error at offset 337 of 338 bytes 重新计算

    $data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':"$2";'", $data); var_dump(unserialize($data)); 输出量

    array 'submit_editorial' => boolean false 'submit_orig_url' => string 'www.bbc.co.uk' (length=13) 'submit_title' => string 'No title found' (length=14) 'submit_content' => string 'dnfsdkfjdfdf' (length=12) 'submit_category' => int 2 'submit_tags' => string 'bbc' (length=3) 'submit_id' => boolean false 'submit_subscribe' => int 0 'submit_comments' => string 'open' (length=4) 'image' => string 'C:fakepath100.jpg' (length=17) 推荐 ..我

    而不是使用这种快速修复方法...我会建议您使用

    您如何序列化数据

    您如何保存它..

    ===============================编辑1 ================ ===============

    错误

    由于使用双引号"而不是单引号而产生了错误',因此将C:\fakepath\100.png其转换为C:fakepath100.jpg

    解决错误

    您需要更改$h->vars['submitted_data']发件人(请特别注意')

    更换

    $h->vars['submitted_data']['image'] = "C:\fakepath\100.png" ; 用

    $h->vars['submitted_data']['image'] = 'C:\fakepath\100.png' ; 附加过滤器

    您还可以在调用序列化之前添加此简单过滤器

    function satitize(&$value, $key) { $value = addslashes($value); }

    array_walk($h->vars['submitted_data'], "satitize"); 如果您具有UTF字符,也可以运行

    $h->vars['submitted_data'] = array_map("utf8_encode",$h->vars['submitted_data']); 如何在将来的序列化数据中检测问题

    findSerializeError ( $data1 ) ; 输出量

    Diffrence 9 != 7 -> ORD number 57 != 55 -> Line Number = 315 -> Section Data1 = pen";s:5:"image";s:19:"C:fakepath100.jpg -> Section Data2 = pen";s:5:"image";s:17:"C:fakepath100.jpg ^------- The Error (Element Length) findSerializeError 功能

    function findSerializeError($data1) { echo "

    "; $data2 = preg_replace ( '!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':"$2";'",$data1 ); $max = (strlen ( $data1 ) > strlen ( $data2 )) ? strlen ( $data1 ) : strlen ( $data2 );

    echo $data1 . PHP_EOL;
    echo $data2 . PHP_EOL;
    
    for($i = 0; $i < $max; $i ++) {
    
        if (@$data1 {$i} !== @$data2 {$i}) {
    
            echo "Diffrence ", @$data1 {$i}, " != ", @$data2 {$i}, PHP_EOL;
            echo "\t-> ORD number ", ord ( @$data1 {$i} ), " != ", ord ( @$data2 {$i} ), PHP_EOL;
            echo "\t-> Line Number = $i" . PHP_EOL;
    
            $start = ($i - 20);
            $start = ($start < 0) ? 0 : $start;
            $length = 40;
    
            $point = $max - $i;
            if ($point < 20) {
                $rlength = 1;
                $rpoint = - $point;
            } else {
                $rpoint = $length - 20;
                $rlength = 1;
            }
    
            echo "\t-> Section Data1  = ", substr_replace ( substr ( $data1, $start, $length ), "<b style=\"color:green\">{$data1 {$i}}</b>", $rpoint, $rlength ), PHP_EOL;
            echo "\t-> Section Data2  = ", substr_replace ( substr ( $data2, $start, $length ), "<b style=\"color:red\">{$data2 {$i}}</b>", $rpoint, $rlength ), PHP_EOL;
        }
    
    }
    

    } 保存到数据库的更好方法

    $toDatabse = base64_encode(serialize($data)); // Save to database $fromDatabase = unserialize(base64_decode($data)); //Getting Save Format 分享来源:stack overflow

    2020-05-10 19:48:08
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载