Page 1 of 1

unserialize turns string into object ??

Posted: Mon Dec 14, 2009 2:32 pm
by LucosidE
can someone please explain this behavior ???

Code: Select all

echo "<br />BEFORE UNSERIALIZE: ".gettype($_SESSION['link']); 
$link = unserialize($_SESSION['link']);       
echo "<br />AFTER UNSERIALIZE: ".gettype($_SESSION['link']);
output :

BEFORE UNSERIALIZE: string
AFTER UNSERIALIZE: object

Why is my input string becoming an object ?? ? ? ?

Re: unserialize ???

Posted: Mon Dec 14, 2009 2:46 pm
by requinix

Code: Select all

$_SESSION["link"];
echo "<br />BEFORE UNSERIALIZE: ".gettype($_SESSION['link']);
$link = unserialize($_SESSION['link']);      
echo "<br />AFTER UNSERIALIZE: ".gettype($_SESSION['link']);
Now what happens?

Re: unserialize ???

Posted: Mon Dec 14, 2009 2:50 pm
by LucosidE
same thing :(

BEFORE UNSERIALIZE: string
AFTER UNSERIALIZE: object

Re: unserialize turns string into object ??

Posted: Mon Dec 14, 2009 2:54 pm
by AbraCadaver
The type of serialized data is always a string, that's the point of serializing it!

If you serialize an object it will be a string. If you unserialize it you'll have your object back.

What did you expect?

Re: unserialize turns string into object ??

Posted: Mon Dec 14, 2009 2:58 pm
by LucosidE
i did not expect the string in session to become an object,

Code: Select all

 
$link = new link_obj(35545);
$_SESSION['link'] = serialize($link);
 
on another page i need to retrieve the object 
$link = unserialize($_SESSION['link']);
i dont see why $_SESSION['link']
is supposed to become an object here.

Re: unserialize turns string into object ??

Posted: Mon Dec 14, 2009 3:05 pm
by AbraCadaver
LucosidE wrote:i did not expect the string in session to become an object,

Code: Select all

 
$link = new link_obj(35545);
$_SESSION['link'] = serialize($link);
 
on another page i need to retrieve the object 
$link = unserialize($_SESSION['link']);
i dont see why $_SESSION['link']
is supposed to become an object here.
I don't know what to tell you. This is exactly what serialize does and why people use it. In your code above:

2. create an object named $link
3. serialize this object and assign it to the session var 'link'
6. unserialize the session var 'link' and assign the object to the var $link

Re: unserialize turns string into object ??

Posted: Mon Dec 14, 2009 3:40 pm
by AbraCadaver
Actually, I missed that you are actually printing the type of the session var. That would make a difference. I get the expected results though (PHP 5.2.4):

Code: Select all

session_start();
$link = new stdClass;
 
$_SESSION['link'] = serialize($link);
echo "<br />BEFORE UNSERIALIZE: ".gettype($_SESSION['link']);
 
//on another page i need to retrieve the object
$link = unserialize($_SESSION['link']);
echo "<br />AFTER UNSERIALIZE: ".gettype($_SESSION['link']);
BEFORE UNSERIALIZE: string
AFTER UNSERIALIZE: string

Re: unserialize turns string into object ??

Posted: Mon Dec 14, 2009 4:56 pm
by LucosidE
This doesn't make any sense at all :(
i think it randomly generates a bool and then based on
that makes it into a string or an object...

PHP Version 5.2.2

Any thoughts people ??
anyone had similar thing happen ?

I strongly dislike Netfirms now

Re: unserialize turns string into object ??

Posted: Tue Dec 15, 2009 9:07 am
by LucosidE
Last night :
:banghead:
give up
:drunk:

today morning
:? :D :?: :?: :?:

I guess the server needed to sleep on it.
Everything works fine today morning

I am still very confused as to why it didn't work
the first place and does now ...

I was using NuSphere to develop and test
and everything was perfect, uploaded to
Netfirms, session was behaving weird and sometimes
perfectly fine mysql queries give errors. seems ok now ...