用户通过身份验证后,我将类对象序列化并将其存储在会话中。但是,即使在使用unserialize()之后,我似乎无法打印其输出。
创建会话变量:
if(!isset($_SESSION['myObj']) && empty($_SESSION['myObj'])) {
$myObj = new User($row['user_id'], $row['username'], $row['user_level']);
$_SESSION['username']= $myObj->usr_name;
$s_Obj = serialize($myObj);
$_SESSION['myObj'] = $s_Obj;
从其他页面访问,无输出:
$sObj = $_SESSION['Obj'];
$myObj = unserialize($s_Obj);
echo $myObj->usr_name;
然而,这是有效的,所以我的班级没有任何问题。我也在两个页面中使用session_start()
。
echo $_SESSION['username'];
班级用户:
<?php
class User{
public $usr_id;
public $usr_name;
public $usr_level;
public $last_access_login;
public function __construct($id, $usr_name, $usr_level) {
$this->usr_id = $id;
$this->usr_name = $usr_name;
$this->usr_level = $usr_level;
$this->last_access_login = date("F d Y H:i:s.",time());
}
}
?>
编辑:
这是我的vardump
array
'myObj' =>
object(__PHP_Incomplete_Class)[1]
public '__PHP_Incomplete_Class_Name' => string 'User' (length=4)
public 'usr_id' => string '3' (length=1)
public 'usr_name' => string 'student' (length=7)
public 'usr_level' => string '3' (length=1)
public 'last_access_login' => string 'November 26 2011 20:12:38.' (length=26)
'url' => string '/researchportal/proposal' (length=24)
答案 0 :(得分:7)
初始页面:
$s_Obj = serialize($myObj);
$_SESSION['myObj'] = $s_Obj;
应该是
$_SESSION['myObj'] = $myObj;
下一页:
$sObj = $_SESSION['Obj'];
$myObj = unserialize($s_Obj);
应该是
$myObj = $_SESSION['myObj'];
此外,请在致电session_start()
之前尝试宣布您的课程。
答案 1 :(得分:2)
您的反序列化对象被反序列化为__PHP_Incomplete_Class_Name
的实例,因为您在反序列化之前未包含User类的定义。见Object Serialization。在尝试反序列化之前,必须包含User类的定义。