在会话中保存对象的问题

问题描述:

In my project, I have this line, in the core file:

$LoggedIn = isset($_SESSION['User']) ? unserialize($_SESSION['User']) : false;

Core: http://pastebin.com/9HMP11bG

and under the login method, it has the corrosponding:

$_SESSION['User'] = serialize($User);

User Repo: http://pastebin.com/vNehc2Hm

User: http://pastebin.com/4UXT79MU

When I try to access the $LoggedIn object, it gives me an error like this:

Fatal error: Uncaught Error: Cannot use object of type __PHP_Incomplete_Class as array in C:\xampp\htdocs\jalawebs\dev\webcofounder\web\controller\index\index.php:14 Stack trace: #0 C:\xampp\htdocs\jalawebs\dev\webcofounder\config\core.php(111): require() #1 C:\xampp\htdocs\jalawebs\dev\webcofounder\index.php(9): require('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\jalawebs\dev\webcofounder\web\controller\index\index.php on line 14

在我的项目中,我在核心文件中有这一行: p>

  $ LoggedIn = isset($ _ SESSION ['User'])?  unserialize($ _ SESSION ['User']):false; 
  code>  pre> 
 
 

核心: http://pastebin.com/9HMP11bG p>

在登录方法下,它有相应的: p>

   $ _ SESSION ['User'] = serialize($ User); 
  code>  pre> 
 
 

用户回购: http://pastebin.com/vNehc2Hm p>

用户: http://pastebin.com/4UXT79MU p>

当我尝试访问$ LoggedIn对象时,它会给出一个如下错误:

 致命错误:未捕获错误:无法在C:\ xampp \ htdocs \ jalawebs \ dev \ webcofounder \ web \ controller \ index \ index.php中使用__PHP_Incomplete_Class类型的对象作为数组 :14堆栈跟踪:#0 C:\ xampp \ htdocs \ jalawebs \ dev \ webcofounder \ config \ core.php(111):require()#1 C:\ xampp \ htdocs \ jalawebs \ dev \ webcofounder \ index.php  (9):require('C:\\ xampp \\ htdocs ...')#2 {main}抛出C语言 :\ xampp \ htdocs \ jalawebs \ dev \ webcofounder \ web \ controller \ index \ index.php第14行
  code>  pre> 
  div>

This happens when you call to a property of an unserialized object that is not defined in the script itself.

Creating a class like so new User() will invoke the autloader to load the script where unserialize() does not.

You could do the following:

$o = unserialize($yourstring);
if(is_object($o) && class_exists(get_class($o))){ // invoke the autoloader
  echo $o->LoggedIn;
}

Or just include the script directly with include_once() or require_once().