0

I am unserializing below string but its not returning anything.

a:57:{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}

Boaz
  • 19,892
  • 8
  • 62
  • 70
  • 1
    There seems a line break after `a:57:`. Is that the case in the original code as well? Also, if you're just echoing the result you might be missing the fact that on failure `unserialize()` returns `false`. Check what exactly the function returns by using `var_dump()` for example. – Boaz Dec 25 '14 at 07:23
  • `Notice: unserialize(): Error at offset 132 of 144 bytes` don't forget display all errors and you will see a notice – Hmmm Dec 25 '14 at 07:26
  • 1
    possible duplicate of [unserialize() \[function.unserialize\]: Error at offset](http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset) – Neo Dec 25 '14 at 07:28
  • No line break, actually. My mistake. But as @CodingInsane says, there's definitely an error. You probably have errors turned off. – Boaz Dec 25 '14 at 07:31
  • Thanks boaz, There is no line break as you said, But var_dump() returns null . What is the solution for this issue? – Janardhanan Dec 25 '14 at 07:31
  • You can check for the actual error here: https://www.functions-online.com/unserialize.html See @Neo duplicate suggestion. – Boaz Dec 25 '14 at 07:42
  • @CodingInsane can you explain what exactly this error means – Janardhanan Dec 25 '14 at 07:59
  • @Boaz I am new to PHP . So can you make that string a working serialized object ? – Janardhanan Dec 25 '14 at 08:37

1 Answers1

1

The data you're unserializing is wrong

a:57{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}

if you try to unserialize it with error reporting E_ALL you will see a notice

$data = 'a:57{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}';

var_dump($data);

you will get

Notice: unserialize(): Error

because

  • a:57 is the array length and from the data you have it's clearly not 57.

  • s: points to the length of string s:10:"Abcdubai" the string Abcdubai is not 10 in length it's 8 so you need to change that to s:8:"Abcdubai"

  • Finally you have s:5:"; at the end for the same reason s:5 means a string with 5 characters in length and it's empty with a one double quote

    <?php
    // this the valid data
     $data = 'a:4:{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:8:"Abcdubai";}';
    
    $data = unserialize($data);
    
    // accessing the valid serialized data
    echo $data['THEME_NAME'];
    echo $data['PUBLIC_ADS_LIMIT'];
    echo $data['PUBLIC_EDIT_LIMIT'];
    echo $data['SITENAME']; 
    

you can try this method to solve formatting issues

   function fixUnserializeFormatting($data){
       // fix string length (will fix s:)
       $data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $data);
       // remove empty matches with one double qoute
       $data = preg_replace('/s\:+[0-9]+\:";/i', '', $data);

       // trying to get the right array length
       $strings = substr_count($data,';') / 2;        
       // fixing array length
       $data = preg_replace('/^a:+[0-9]+:/i', "a:{$strings}:", $data);

       // finally returning the formatted data
       return $data;    
   }

Usage

   $data = 'a:57:{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}';

   $data = fixUnserializeFormatting($data);

   var_dump(unserialize($data));       
Hmmm
  • 1,774
  • 2
  • 13
  • 23