php Codeigniter 当前登录用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4587750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:35:22  来源:igfitidea点击:

Codeigniter Current Logged in user

phpcodeigniteractiverecord

提问by Brad

In Codeigniter, I have a login/registration form all set up, working perfectly, however how can I output the current users username, i.e. When the user has logged in it would say "Welcome, [username]".

在 Codeigniter 中,我设置了一个登录/注册表单,运行良好,但是如何输出当前用户的用户名,即当用户登录时,它会说“欢迎,[用户名]”。

I also need to know this, to be able to output from the database the record based on the current users ID, how can I do that? I've browsed the docs on codeigniter but couldn't find what i was looking for. I'm using active record btw.

我也需要知道这一点,为了能够根据当前用户 ID 从数据库中输出记录,我该怎么做?我浏览了 codeigniter 上的文档,但找不到我要找的东西。顺便说一句,我正在使用活动记录。

Thanks

谢谢

回答by Brad

echo $this->session->userdata('name')

Incorporate that into the db select files as well, for instance if you have files that they created and you want to show them when they log in, use the session data in the db call as a where

也将其合并到 db select 文件中,例如,如果您有他们创建的文件并且您想在他们登录时显示它们,请使用 db 调用中的会话数据作为 where

I suggest you put all session data in the database as shown in session class.

我建议您将所有会话数据放入数据库中,如会话类所示。

http://codeigniter.com/user_guide/libraries/sessions.html

http://codeigniter.com/user_guide/libraries/sessions.html

回答by bensiu

How is your login form performing the login action ? In most cases it's using session to identify that you are logged in. After you confirm (usually with database) that a user is OK drop user info in session (ID, readable name, and any other things you need) and next:

您的登录表单如何执行登录操作?在大多数情况下,它使用会话来识别您已登录。在您确认(通常使用数据库)用户正常后,在会话中删除用户信息(ID、可读名称和您需要的任何其他内容),然后:

echo "Welcome " . $_SESSION["username"];