Richard Knop Richard Knop - 7 months ago 50
PHP Question

Unserialize sometimes returns false

I have this function in my application:

public function direct($theTree)
{
$aTreeRoot = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $theTree);
return unserialize($aTreeRoot);
}


It should never return false but in the error logs the error keep occuring which says it returned false.

However, I cannot replicate the error in my application. I'm trying to every possible way but it always works.

Is there something wrong with the function?

The
$theTree
comes from session.

Edit: The regex is there because:
unserialize
- Search for my regex there in the comments. It's supposed to solve a problem.

Answer

I have faced similar kind of issue earlier. I show u how i have solved it.

After you serialize data, apply base64_encode() e.g

$txt = base64_encode(serialize($txt));

And when you unserialize it

e.g.

 $txt = unserialize(base64_decode($txt));

Try this . Hope work for u as well. Good luck