简体   繁体   中英

How can I show data user currently logged in in CodeIgniter

I tried to show the data "role_name" column in the role table. otherwise in the user table have "role_id" which that was a foreign key to the "id" column at role table. but when I call it the data was null.

This is my controller:

class Member extends CI_Controller {

  public function index(){
    $data['title'] = 'My Profile';
    $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
    $data['role'] = $this->db->get_where('role', ['id' => $this->session->userdata('role_id')])->row_array();
    var_dump($data);
    die;

and this is the view:

array(3) { ["title"]=> string(10) "My Profile" ["user"]=> array(11) { ["id"]=> string(1) "7" ["name"]=> string(10) "Imal Malik" ["email"]=> string(14) "imal@gmail.com" ["password"]=> string(60) "$2y$10$muXwl9cdeqc0aq45tEkGe.6hp.mRSX4wnz5RpRcGQIhQS8RbUGM1C" ["image"]=> string(11) "default.jpg" ["date"]=> string(10) "1570253690" ["role_id"]=> string(1) "2" ["active"]=> string(1) "1" ["phone"]=> string(12) "087666222333" ["place"]=> string(7) "Bandung" ["about"]=> string(52) "Hai! I'am Malik, I have been Programmer Since 1900s." } ["role"]=> NULL }

As an alternative, you could use role_id data from the retrieved user record instead of using role_id from a session:

  public function index(){
    $data['title'] = 'My Profile';
    $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
    $data['role'] = $this->db->get_where('role', ['id' => $data['user']['role_id']])->row_array();
    var_dump($data);
    die;

Or if you prefer to use join instead:

    $this->db->select('*');
    $this->db->from('user');
    $this->db->join('role', 'user.role_id = role.id');
    $this->db->where('user.email', $this->session->userdata('email'));
    $data = $this->db->get();

The most efficient way would be to store those pieces of data within the session variables upon login. Then, you can access it at all times by simply using $this->session->userdata() calls anywhere in your site and save a lot of database querying on each page load (this may seem like not much of a saving if your site is low traffic, but if you have hundreds of concurrent users, each loading multiple pages per session, you are looking at savings of thousands of database queries per hour)

Upon successful log-in you could, for example, build and array with all the session data you may need. For example:

$sess_data = array(
  'user_id' => $user_id,
  'name' => $name,
  'last_name' => $last_name,
  'email' => $email,
  'access_level' => $access_level
);

and then set push the whole array into the session data like this:

$this->session->set_userdata('logged_in', $sess_data);

afterwards, if you need the user's name, just echo $this->session->userdata['logged_in']['name'];

(yes, I use a logged_in array within the session data to separate the user data from other session variables that go in a separate array within the session... but it's not mandatory to do so)

since the session is active, the results will come from memory and not from a database query, thus removing a lot of overhead from your site. It's a little bit more work to set this up, but you do it once in your log-in routine and then just forget about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM