Re: passing an object using session var
| From: | list arama | Date: | Wed, 29 Nov 2006 18:36:26 +0000 |
| Subject: | Re: passing an object using session var | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-245295@lists.php.net to get a copy of this message | ||
errata:
$newObj->display(); // this *does* workOn 11/29/06, list arama <listarama@googlemail.com> wrote:
Here's what I came up with: --script1--- <?php include('TestClass.php'); session_start(); $obj = $_SESSION['obj'];// session_register($obj); // recommended not to use - said to beoutdated usage ob_start(); $obj = &new TestClass(); $obj->setName('MyName'); $obj->display(); header("Location: http://localhost/~mdl/webProj/photoAlbum2/receive.php <http://localhost/%7Emdl/webProj/photoAlbum2/receive.php>"); ob_flush(); ?> ---script2--- <?php include("TestClass.php"); session_start();$newObj = $_SESSION['obj']; // this works//$_SESSION['obj'] = $newObj; // this doesn't work$_SESSION['obj']->display(); // $newObj->display(); // this doesn't $_SESSION = array(); // unsets session having effect that script failson every second call ?> My only remaining question is why the session fails every second time if the session is emptied and destroyed. Shouldn't script1 initialize a totally new session and populate with fresh session vars? On 11/29/06, Eric Butera <digital.tarsier@gmail.com> wrote: On 11/29/06, Jochem Maas <jochem@iamjochem.com> wrote:Thomas Munz wrote:( see php.net manual ) in order to make an object working via sessionTry 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 objectDONT SERIALIZE! its done automaticallysession,note: Use $_SESSION instead of session_register();$_SESSION['obj'] = $str_object: //-- save serialized string in thehttp://localhost/~lr/webProj/photoAlbum2/sessionReceiveObj.php<http://localhost/%7Elr/webProj/photoAlbum2/sessionReceiveObj.php>"); // re-direct to script2header("Location:into object---script 2---- <?php include("TestClass.php"); session_start(); $newObj = unserialize($_SESSION['obj']); //-- convert string backDONT UNSERIALIZE! its done automaticallysession_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$newObj->display(); // MyNameon 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');ob_start(); $obj = new TestClass(); $obj->setName('MyName');$obj->display(); // outputs 'MyName'header("Location: http://localhost/~lr/webProj/photoAlbum2/sessionReceiveObj.php<http://localhost/%7Elr/webProj/photoAlbum2/sessionReceiveObj.php>"); //It might be worth noting that if you are using php4 remember to pass your object by reference when setting/retrieving from the session. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpre-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?>