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;
$用戶是用戶的一個類的實例
現在這是什麼的print_r($ _ SESSION [」登錄'])顯示了我:
__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」它返回一個空值給我。
[「在會話變量中存儲PHP對象」(http://stackoverflow.com/questions/2042271/storing-a-php-object-in-a-session-variable),[「 PHP會話與一個不完整的對象「](http://stackoverflow.com/questions/1055728/php-session-with-an-incomplete-object),[」PHP會話對象的問題「](http:// stackoverflow。 COM /問題/ 5348457 /問題與-PHP會話對象)。 – outis
參見[「PHP:存儲‘對象’的$ _SESSION內部」(http://stackoverflow.com/questions/132194/php-storing-objects-inside-the-session),[「我能保存的對象,在會議?「](http://www.phpriot.com/articles/intro-php-sessions/8)。 – outis
我不會那樣做。例如,如果用戶使用另一臺計算機在另一臺計算機上登錄時更改其電子郵件,則最終可能會收到舊數據。或者,如果您從數據庫中刪除用戶,您的應用程序仍然會認爲用戶存在。相反,我只會在會話中存儲用戶標識,並在處理來自用戶的請求時從數據庫中檢索其餘信息。 – shesek