Richard Knop Richard Knop - 10 months ago 82
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?

comes from session.

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


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


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

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