docs:programming:php:storing_objects_in_sessions

Storing Objects in Sessions

  • you must load the class definition files prior to calling session_start()
  • I couldn't find documentation of this anywhere, but it seems that you can't name your session variable the same as the variable of the object, or PHP will remove this from the session when you call unserialize($_SESSION['varname'])
// create the object
$objVar = new SomeObj();
 
// serialize and store the object in the session
$_SESSION['serObjVar'] = serialize($objVar);
 
...
 
// load the Class definition file
require "SomeObj.php";
 
// begin the session
session_start();
 
// unserialize the object into a usable variable
$objVar = unserialize($_SESSION['serObjVar']);
  • docs/programming/php/storing_objects_in_sessions.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1