У меня есть объект, основанный на дизайне Singleton, который я использую для аутентификации пользователя. Поскольку объект является объектом для каждого пользователя, я хочу, чтобы этот объект автоматически сохранялся в переменной сеанса в конце выполнения. Однако всякий раз, когда я пытаюсь сериализовать объект, как внутри, так и снаружи, я получаю пустую строку.
Ниже приведен базовый класс, за исключением несущественных функций:
<?php
/**
* The user class is intended to handle information about authenticated users. Information contained in this class is to be stored
* in SESSION['session_user'] as a serialized object.
*/
class User {
// Reference to the single User instance
private static $_instance;
// User levels
const GUEST = 0;
const USER = 1;
const ADMINISTRATOR = 3;
// Information about the account
private $_username;
private $_userid;
private $_userlevel;
// Information about the user, for preventing session hijacking
private $_ipaddress;
private $_useragent;
private function __construct() {
// Set the visitor's information
// Set the default information
}
public static function getUser() {
// Check if a user object has been created
if (!isset(self::$_instance)) {
// Check if the object is stored in the user session
if (isset($_SESSION['session_user'])) {
self::$_instance = unserialize($_SESSION['session_user']);
//unset($_SESSION['session_user']);
echo 'Unserializing user';
} else {
$c = __CLASS__;
self::$_instance = new $c;
echo 'Creating new user';
}
}
return self::$_instance;
}
function __wakeup() {
// First, check that the user agent has not changed
// Check that the IP has not changed
}
function __destroy() {
$_SESSION['session_user'] = serialize(self::$_instance);
echo serialize(self::$_instance);
print_r($_SESSION);
}
public function __set($index, $value) {
return NULL;
}
public function __get($index) {
// Determine which value to return
}
public function authenticate($inUsername, $inPassword) {
// Authenticate the user
}
}
?>
Каждый раз, когда я вызываю serialize объекта, либо внутри метода __destroy с использованием serialize ($ this) или serialize (self :: $ _ instance), либо извне с использованием serialize ($ user), я получаю пустую строку. Тем не менее, я знаю, что объект существует, так как я могу извлечь из него данные о прошедшем проверку пользователе.