Codeigniter登录后如何获取用户ID [英] Codeigniter how to get userid after login

查看:109
本文介绍了Codeigniter登录后如何获取用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功创建了注册和登录系统.
我已经在登录表单中使用了useremail和password,并且想要显示用户名和与该登录用户相关的其他属性.

I have successfully created a registration and login system.
I have used useremail and password in the login form and I want to display the username and other properties related to that logged in user.

登录codeigniter后获取用户ID的最简单和最佳做法是什么?

What are the simplest and best practices to get the userid after a login in codeigniter?

推荐答案

作为优秀的编码人员,请在登录时创建session,并在网站范围内使用session.

As good coder create session at login time and use session at website wide.

public function login($username, $password) {
    $user = $this->db
        ->select("username, name, phone")
        ->where(
             [
                'username' => $username,
                'password' => md5($password)
             ]
         )
        ->get("table_name")
        ->row();

   if ($user) {
         $logindata = [
             'userid' => $user->username,
             'name'   => $user->name,
             'phone'  => $user->phone
         ];
         $this->session
              ->set_userdata($logindata);
         return true;
   }
   else {
         return false;
   }
} 

然后您便可以在网站中的任何地方使用

Then after you can use anywhere in website

echo $this->session->userid;

我希望它能在总体上为您提供帮助.

I hope it will help you in general way.

这篇关于Codeigniter登录后如何获取用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆