Welcome Guest, Not a member yet? Register   Sign In
Loading objects from session
#1

[eluser]Unknown[/eluser]
Here is a problem I got that I couldn't find an answer to despite RTFM and "Google is your friend".

Summary of the problem:
PHP incomplete object/class when loading object from CodeIgniter sessions on a refreshed page.

Goal:
To use objects together with CodeIgniter sessions, if possible.

Description:
The problem is that I try to store an user object in the CodeIgniter session. As we will see, it works fine at the first page but later it returns __PHP_Incomplete_Class and __PHP_Incomplete_Class_Name messages.

First we have the login-function in the controller c_user.php
Code:
public function login($username, $password)
{
  //Creating an user object.
  $this->load->model('m_user', 'user');
  
  //Trying to login the user.
  $success = $this->user->login($username, $password);
  
  //Printing the result.
  if ($success) {
   //Creating a second user object (to see if it is possible to load object from CodeIgniter session).
   $user2 = $this->session->userdata('user');
  
   //Dumping the user2 variable to see what we got.
   echo "<pre>";
   var_dump($user2);
   echo "</pre>";
  
   //The following row successfully prints the name of the loged in user.
   echo "<p>The user " . $user2->info['name'] . " was loged in!</p>";
  }
  else
   echo "The user was NOT loged in!";
}
When I access this function by the URL .../index.php/c_user/login/testuser/silly_password it returns a dump...
Code:
object(M_user)#19 (1) {
  ["info"]=>
  array(4) {
    ["password"]=>
    string(128) "c90d1756773dbe641f3eb88478da74f65ffd0c4e9aa9d8115236cccff9aa4a79e7df136b06e97723060af4093c7ec1af004a638dd33bef30eeb7a3fb10fde62b"
    ["salt"]=>
    string(40) "71bbc44a661a54e4cd0378dc31d9462665517260"
    ["signup_date"]=>
    string(19) "2011-11-30 19:09:23"
    ["name"]=>
    string(5) "testuser"
  }
}
... and the text The user testuser was loged in!, so you might think loading objects from the session works.

The function $this->user->login($username, $password) does some checking, loads data into the array variable of the object and then puts the object into the session by the code below.
Code:
$this->session->set_userdata('user', $this);

But...

Then I try to use the update-function in the same controller (by the URL .../index.php/c_user/update).
Code:
public function update()
{
  //Loading the user model.
  $this->load->model('m_user');
  
  //Trying to load the user object from the session.
  $user = $this->session->userdata('user');
  
  //Dumping the user variable to see what we got.
  echo "<pre>";
  var_dump($user);
  echo "</pre>";
  
  //Trying to print the username in the same style.
  echo "<p>The username is " . $user->info['name'] . ".</p>"; //This is row 59.
}

The result is a dump...
Code:
object(__PHP_Incomplete_Class)#18 (2) {
  ["__PHP_Incomplete_Class_Name"]=>
  string(6) "M_user"
  ["info"]=>
  array(4) {
    ["password"]=>
    string(128) "c90d1756773dbe641f3eb88478da74f65ffd0c4e9aa9d8115236cccff9aa4a79e7df136b06e97723060af4093c7ec1af004a638dd33bef30eeb7a3fb10fde62b"
    ["salt"]=>
    string(40) "71bbc44a661a54e4cd0378dc31d9462665517260"
    ["signup_date"]=>
    string(19) "2011-11-30 19:09:23"
    ["name"]=>
    string(5) "testuser"
  }
}
... an error message (see attached file php_error.png) and the text The username is .

Solutions tried:
Some Googling told me to autoload the m_user model which I tried, but the result was the same.

I also tried to change the code slightly, like in the update-function using

Code:
$this->load->model('m_user', 'user')
$this->user->info['name']
instead of
Code:
$this->load->model('m_user')
$user->info['name']
Same result.

Final questions:
Is it actually possible to use objects and sessions in this way?
If yes, how to get rid of the "PHP incomplete object"-problem?
Does the problem come from something obvious that I missed?
Was the problem described clear enough or does it need some clarification?




Theme © iAndrew 2016 - Forum software by © MyBB