Sometimes Unserialize 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 the error is saved in the error logs, which says that it returns false.

However, I cannot reproduce the error in my application. I try my best, but it always works.

Is there something wrong with the function?

$theTree comes from a session.

Edit: A regex exists because: unserialize - Find my regex there in the comments. He must solve the problem.

+4
source share
4 answers

I came across a similar question earlier. I show how I decided it.

After serializing the data, apply base64_encode () for example,

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

And when you unserialize it

eg.

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

Try it. Hope works for you too. Good luck.

+37
source

Is the magic_quotes_gpc value the same both in production and on your local machine?

+1
source

I got some random behavior in my code, but I think I found out why. I used UTF-8 encoding, and on my production server it seems these problems are occurring. Try the following:

 $txt = unserialize(utf8_encode($aTreeRoot)); 

Worked for me, I hope it will be for you too.

+1
source

I believe that escaping the data you serialize will also work as an alternative to base64.

 $data = serialize($results); $encoded = htmlentities($data); echo '<input type="hidden" name="data" value="'.$encoded.'">'; 
0
source

All Articles