Question: How do you access a logged-in user's data for use throughout the site?
The logged-in user specific data is usually stored in a session. For codeigniter, you can access session data using $this->session->userdata('item');, or for example $this->session->userdata('username');.
However, if you are using a specific login system it depends on the login system that you are using.
Custom Login System
After receiving the login information from a login form, pass the data to the session using the following:
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
Then refer to the username with the following:
$this->session->userdata('username');
See Adding Custom Session Data in the Codeigniter Documentation.
Login System Example
A popular codeigntier authentication library is Tank-Auth.
When using Tank-Auth, after a user is logged in, you can call $this->tank_auth->get_username() (which calls $this->ci->session->userdata('username'), as mentioned above). This can then be stored and passed to a view.
In Tank-Auth you will see:
application/controller/welcome.php
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$this->load->view('welcome', $data);
application/views/welcome.php
Hi, <strong><?php echo $username; ?></strong>! You are logged in now.