Is it possible to put an object into a session variable?
I'm working on my user system and I'd like to make it fairly easy to manage, so, I decided to go with my new best friend, OOP, and make a class out of it.
<?php
// Users class (this is just an example, not my code)
class user {
var $username = 'Guest';
var $access = 1; // default guest access
var $cur_page;
var $last_page;
function login() {
// log the user in
}
function setpage($page) {
$this->last_page = $this->cur_page;
$this->cur_page = $page;
}
function debug() {
echo "<pre>";
print_r($this);
echo "</pre>";
}
?>
Now I wanted a way to keep that object trhough all of my pages, the only think I came up with was to use a session
<?php
session_start();
require_once(dirname(__FILE__).'/libs/user.class.php');
$_SESSION['user'] = (isset($_SESSION['user'])) ? $_SESSION['user'] : new user;
$current_page = 'http://'.$_SERVER['SERVER_ADDR'].$_SERVER['REQUEST_URI'];
// Set the current page
$_SESSION['user']->setpage($current_page);
// Lets see whats happening inside...
$_SESSION['user']->debug();
?>
Ok, now here is where everything has started to confuse me:
Here is the output the FIRST time I visit that page:
user Object
(
[username] => Guest
[access] => 1
[last_page] =>
[cur_page] => [url]http://127.0.0.1/[/url]
)
And the second time (or any time after):
Fatal error: main() [function.main]: The script tried to execute a method or access a property of an incomplete object.
Please ensure that the class definition "user" of the object you are trying to operate on was loaded _before_
unserialize() gets called or provide a __autoload() function to load the class definition in
\Apache2\htdocs\setup.php on line 32
I have noooo clue whats going on, but I'm guessing its something to do with the way sessions are encoded, but I've never really looked at that kind of thing so I wouldn't know :S
Anyways... Please help if you can, thanks!