How to repair a serialized string which has been corrupted by an incorrect byte count length?

后端 未结 15 2084
后悔当初
后悔当初 2020-11-22 11:46

I am using Hotaru CMS with the Image Upload plugin, I get this error if I try to attach an image to a post, otherwise there is no error:

unserialize()

15条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 12:28

    Here is an Online Tool for fixing a corrupted serialized string.

    I'd like to add that this mostly happens due to a search and replace done on the DB and the serialization data(specially the key length) doesn't get updated as per the replace and that causes the "corruption".

    Nonetheless, The above tool uses the following logic to fix the serialization data (Copied From Here).

    function error_correction_serialise($string){
        // at first, check if "fixing" is really needed at all. After that, security checkup.
        if ( unserialize($string) !== true &&  preg_match('/^[aOs]:/', $string) ) {
             $string = preg_replace_callback( '/s\:(\d+)\:\"(.*?)\";/s',    function($matches){return 's:'.strlen($matches[2]).':"'.$matches[2].'";'; },   $string );
        }
        return $string;
    } 
    

提交回复
热议问题