Re: passing an object using session var
| From: | Jochem Maas | Date: | Wed, 29 Nov 2006 16:08:37 +0000 |
| Subject: | Re: passing an object using session var | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-245284@lists.php.net to get a copy of this message | ||
Thomas Munz wrote:
> Try that:
>
> --script1---
> <?php
> include("TestClass.php");
> session_start();
>
> $obj = new TestClass();
> $obj->setName('MyName');
> $obj->display(); // outputs 'MyName'
>
> $str_object = serialize($obj); //-- You need to serialize an object ( see php.net manual ) in
> order to make an object working via session
DONT SERIALIZE! its done automatically
>
> $_SESSION['obj'] = $str_object: //-- save serialized string in the session,note: Use
> $_SESSION instead of session_register();
>
> header("Location:
> http://localhost/~lr/webProj/photoAlbum2/sessionReceiveObj.php");
> // re-direct to script2
>
> ---script 2----
> <?php
> include("TestClass.php");
> session_start();
>
> $newObj = unserialize($_SESSION['obj']); //-- convert string back into object
DONT UNSERIALIZE! its done automatically
> $newObj->display(); // MyName
>
>
> on Wednesday 29 November 2006 16:18, list arama wrote:
>> Why am I not able to access the obj name field in script 2?
>>
>> thanks in advance,
>>
>> --script1---
>>
>> <?php
>>
>> include("TestClass.php");
>>
>> session_start();
>> session_register('obj');
session_register() et al are going the way of the dino.
just use $_SESSION directly, just remember to call session_start()
before you try to use $_SESSION
>>
>> ob_start();
>>
>> $obj = new TestClass();
>>
>> $obj->setName('MyName');
>> $obj->display(); // outputs 'MyName'
>>
>> header("Location:
>> ìô); //
>> re-direct to script2
>>
>> ob_flush();
>>
>> ?>
>>
>> ---script 2----
>>
>> <?php
>>
>> include("TestClass.php");
>>
>> session_start();
>>
>> $newObj = $HTTP_SESSION_VARS['obj'];
>>
>> $newObj->display(); // no output, meaning no data?
>>
>> $newObj->setName('Michael');
>> $newObj->display(); // outputs 'Michael' using obj methods
>>
>> ?>
>