Search code examples
phpcodeignitersessioncodeigniter-3

Codeigniter 3 application bug: unable to store certain column value in a session


I am working on a Social Network application with Codeigniter 3, Ion-Auth and Bootstrap 4. You can see the Github repo HERE.

I want to store the first_name of the logged in user into the current session. For this purpose, to the original $session_data array, I have added the line 'user_first_name' => $user->first_name,

$session_data = [
        'identity'                 => $user->{$this->identity_column},
         $this->identity_column     => $user->{$this->identity_column},
        'email'                    => $user->email,
        'user_id'                  => $user->id,
        'user_first_name'          => $user->first_name, //retrieve from the first_name column
        'old_last_login'           => $user->last_login,
        'last_check'               => time(),
        'ion_auth_session_hash'    => $this->config->item('session_hash', 'ion_auth'),
];

The point of that is to show a welcome message once the user id logged in:

Welcome, <?php echo $this->session->userdata('user_first_name'); ?>

I get am Undefined property $first_name message and the welcome message is consists nothing but "Welcome," (despite he fact that $this->session->userdata('user_first_name') does return the user's id).

What is missing?


Solution

  • A Quick examination of what $user in Ion_auth_model.php , set_session() is using var_dump shows it to be...

    LINE: 1944 Module Ion_auth_model
    object(stdClass)#26 (5) {
      ["email"]=>
      string(21) "frednurk@gmail.com"
      ["id"]=>
      string(1) "8"
      ["password"]=>
      string(60) "$2y$10$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"
      ["active"]=>
      string(1) "1"
      ["last_login"]=>
      string(10) "1599322333"
    }
    

    Which does not contain first_name. Which is what you were expecting due to your error message.

    Searching where this is called, will lead you back to Ion_auth_model.php - login, where the select query for $user is

    $query = $this->db->select($this->identity_column . ', email, id, password, active, last_login')
                      ->where($this->identity_column, $identity)
                      ->limit(1)
                      ->order_by('id', 'desc')
                      ->get($this->tables['users']);
    

    And as expected it's missing first_name.

    So adding first_name into the Select like so...

    $query = $this->db->select($this->identity_column . ', email, id, password, active, last_login, first_name')
                      ->where($this->identity_column, $identity)
                      ->limit(1)
                      ->order_by('id', 'desc')
                      ->get($this->tables['users']);
    

    Results in $user becoming...

    LINE: 1944 Module Ion_auth_model
    object(stdClass)#26 (6) {
      ["email"]=>
      string(21) "frednurk@gmail.com"
      ["id"]=>
      string(1) "8"
      ["password"]=>
      string(60) "$2y$10$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"
      ["active"]=>
      string(1) "1"
      ["last_login"]=>
      string(10) "1599322333"
      ["first_name"]=>
      string(3) "Tim"
    }
    

    And then it's all happy.

    Conclusion

    1. Read the Error messages
    2. Perform Debug by inspecting your variables/array/objects to see what they are.
    3. Work back and fix it.

    When debugging, I have this little code snippet I use

    echo '<pre>';
    echo 'LINE: '. __LINE__. ' Module '.__CLASS__.'<br>';
    var_dump(WHAT_TO_LOOK_AT_GOES_HERE);
    echo '</pre>';