Codeigniter ion auth trying to get the user name of a logged in user in to a string

$this->ion_auth->user() is a DB object.

$this->ion_auth->user()->row(); returns the user object which you can query for first_name, number_of_cats_owned, or whatever.

https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/2/controllers/auth.php


Ion Auth sets the username, email, user id, and last login time in session variables when the user logs in (have a look at ln 683 ish in the ion_auth_model's login function.)

The simplest way to get this info is by using CI's session class like so:

$username = $this->session->userdata( 'username' );

Of course, I abuse helpers so to make this even simpler I'll normally use a variant of this helper function:

function userdata( $key, $val = null ){
  $ci = &get_instance();
  if ( $val !== null ){
    $ci->session->set_userdata( $key, $val );
  } else {
    return $ci->session->userdata( $key );
  }
}

This gives us a handy (& global scoped) function to call on as needed, wherever needed:

$username = userdata( 'username' );