Page 1 of 1

PHP object as session variable

Posted: Fri Oct 17, 2008 5:55 am
by nobleknight
Hello,

can someone tell me how to pass an object (an instance of a php built-in class) as a session variable?

Thanks

Re: PHP object as session variable

Posted: Sat Oct 18, 2008 8:50 pm
by ahowell

Code: Select all

<?php
 
// Insert
$_SESSION['someIdentifier'] = serialize($object);
 
 
 
// Extract
require_once '/path/to/your/class.php';
$object = unserialize($_SESSION['someIdentifier']);
Notes:
- Storing your object in a session, it is no longer an 'instance'. Any changes occurring
to the object after it is serialized will not be contained in the serialized version. Make sure you only
save your objects to the session at the very end of all processing.

- Do not use session.auto_start when storing objects within session. Class files need to be included
before the objects can be brought out of a session. Auto start will take place before you would be able
to include all necessary files.

Re: PHP object as session variable

Posted: Sun Oct 19, 2008 3:49 am
by VladSun
Objects stored in $_SESSION variable are serialized automatically. There is no need to use un/serialize() functions.

Code: Select all

$_SESSION['myobject'] = $object;
........
$next_page_var = $_SESSION['myobject'];