php – 我可以在$_SESSION空间中存储一个类实例吗?

前端之家收集整理的这篇文章主要介绍了php – 我可以在$_SESSION空间中存储一个类实例吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Class User{

public $id;
public $username;
public $password;
public $email;
public $steam;
public $donator;
public $active;

public function __construct($username,$email,$password,$id,$active,$donator,$steam){
    $this->id = $id;
    $this->username = $username;
    $this->password = $password;
    $this->email = $email;
    $this->steam = $steam;
    $this->donator = $donator;
    $this->active = $active;
}}

是我的课(简化)

以下是我的代码

$_SESSION['loggedIn'] = $user;

$user是User的类实例

现在这是print_r($_ SESSION [‘loggedIn’])显示内容

__PHP_Incomplete_Class Object
(
    [__PHP_Incomplete_Class_Name] => User
    [id] => 22
    [username] => xxxx
    [password] => xxxx
    [email] => xxxx
    [steam] => 1234567
    [donator] => 0
    [active] => 1
)

其中xxxx是正确的值.

但是当我尝试从我的会话中检索数据.像这样:“$_SESSION [‘loggedIn’] – > username”它返回一个空值给我.

您必须首先将对象序列化为字符串:

$_SESSION [‘user’] = serialize($user);

和:

$user = unserialize($_ SESSION [‘user’]);

只需确定在重新排序对象之前首先定义该类.

原文链接:https://www.f2er.com/php/131722.html

猜你在找的PHP相关文章