Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 619
Richard Knop Avatar asked Nov 24 '10 07:11

Richard Knop


People also ask

What is unserialize function in PHP?

The unserialize() function converts serialized data back into actual data.

How do you unserialize data in MySQL?

The best approach is to have normalized table structure (different fields or tables). The next approach is to save data as a delimited string (e.g.: 0,US,1,19 ). Then you can use MySQL's SUBSTRING() or to use standard serialization mechanisms like JSON encode. thats right but the value is not inserted by external app.

How do I unserialize laravel data?

serialize is just a built-in, variable handling, PHP function. The counterpart of this is unserialize. You have to save the unserialized data in a variable -> $data=unserialize($serializedData) . After that you can access the data by using the index like $data["name"] for instance.

What is __ Php_incomplete_class?

Generally when trying to store objects in the session, in files or pass them through sockets, the object can be referenced as being of the __PHP_Incomplete_Class class, this is because the correct way to store and retrieve an object in session and also in other cases) is to use the functions serialize() and unserialize ...


3 Answers

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

like image 132
Poonam Bhatt Avatar answered Oct 14 '22 06:10

Poonam Bhatt


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

like image 34
racetrack Avatar answered Oct 14 '22 06:10

racetrack


I got some random behaviour on my code, but I think I found out why. I was using UTF-8 charset, and in my production server, it seems to produce these issues. Try this:

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

Worked for me, hope it will for you too

like image 27
Federico Giraldi Avatar answered Oct 14 '22 06:10

Federico Giraldi