6

Once the user is authenticated, I serialized the class object and stored it in a session. However, I cant seem to print its output even after using unserialize().

Creating the session variable:

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;

Accessing from a different page, no output:

$sObj = $_SESSION['Obj'];
$myObj = unserialize($s_Obj);
echo $myObj->usr_name;

This works however, so there's nothing wrong with my class. I am also using session_start() in both pages.

echo $_SESSION['username'];

Class user:

<?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());
  }
}
?>

Edit:

This is my 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)
user478636
  • 3,304
  • 15
  • 49
  • 76
  • Did you use `session_start()` on all of your pages? And what does `User()` look like? – Kevin Ji Nov 26 '11 at 20:00
  • `$_SESSION['myObj'] = $myObj` should be enough, I think -- PHP should handle the serialization for you. – lonesomeday Nov 26 '11 at 20:01
  • @lonsesomeday I also tried that, but it didnt work. – user478636 Nov 26 '11 at 20:02
  • 4
    There is no point in serializing data, then storing it in a session. Session data is implicitly serialized, you are just adding processing overhead. Just do `$_SESSION['myObj'] = $myObj`. This may or may not fix the problem - it's just general advice. Not that you would need to include the file that contains the class `User` *before* calling session_start(). – DaveRandom Nov 26 '11 at 20:02
  • Always turn turn up error reporting to the max when developing. Then var_dump($_SESSION); Then read about defining classes *before* calling session_Start() – goat Nov 26 '11 at 20:20
  • Duplicates "[PHP Session with an Incomplete Object](https://stackoverflow.com/q/1055728/90527)", "[PHP __PHP_Incomplete_Class Object with my $_SESSION data](https://stackoverflow.com/q/2010427/90527)", "[Storing A PHP Object In A Session Variable](https://stackoverflow.com/q/2042271/90527)", "[Problem with PHP Session Object](https://stackoverflow.com/q/5348457/90527)" and others. – outis Dec 18 '17 at 23:52

2 Answers2

7

Inital page:

$s_Obj = serialize($myObj);
$_SESSION['myObj'] = $s_Obj;

should be

$_SESSION['myObj'] = $myObj;

Next page:

$sObj = $_SESSION['Obj'];
$myObj = unserialize($s_Obj);

should be

$myObj = $_SESSION['myObj'];

In addition, try declaring your classes before your call to session_start().

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
3

Your unserialized object is being unserialized as an instance of __PHP_Incomplete_Class_Name because you are not including the definition of the User class prior to unserializing it. See Object Serialization. You must include the definition of the User class before you try to unserialize it.

John Watson
  • 2,554
  • 1
  • 17
  • 13