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.
The unserialize() function converts serialized data back into actual data.
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.
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.
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 ...
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
Is the value of magic_quotes_gpc
the same both on the production, and your local machine?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With