Hi team i am new to CI and ion auth and i want to find the best way to get the user name on the logged user and make it a string. I have try this and im getting an error.
Object of class CI_Email could not be converted to string
This is my Controller
class Dashboard extends CI_Controller {
function index() {
$user = $this->ion_auth->user();
$email = $user->email;
$data['email']= $email;
$data['mainContent'] = 'dashboard_view';
$this->load->view('template', $data);
}
and view
<div id='dashboarWap'>
<?php echo form_open('dashboard/dashInput'); ?>
<?php echo $email; ?>
Any idea on why i am getting this error would be a big help thanks.
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' );
$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With