[英]How can I access data in my database? - CodeIgniter
I am using CodeIgniter and have a login system. 我正在使用CodeIgniter并具有登录系统。 I am still fairly new with php and I am wondering once the user is logged in, how I can access his/her data to use throughout the site?
我对php还是很陌生,我想知道一旦用户登录,我如何访问他/她的数据以在整个站点中使用? Right now I am trying to make it say welcome "username" but I dont know where the variables come from and how I should use them.
现在,我试图让它说“欢迎”“用户名”,但我不知道变量来自何处以及如何使用它们。 Right now in my home_view.php I have:
现在在我的home_view.php中,我有:
<div class="main">
<h2>Welcome <?php echo $username; ?>!</h2>
</div>
It throws this error: 它引发此错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: username
Filename: views/home_view.php
Line Number: 22
Can someone tell me how to do this? 有人可以告诉我该怎么做吗?
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');
对于codeigniter,您可以使用
$this->session->userdata('item');
访问会话数据$this->session->userdata('item');
, or for example $this->session->userdata('username');
,例如
$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' => 'johndoe@some-site.com',
'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. 请参阅Codeigniter文档中的添加自定义会话数据 。
Login System Example 登录系统示例
A popular codeigntier authentication library is Tank-Auth . 流行的codeigntier身份验证库是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). 使用Tank-Auth时,在用户登录后,可以调用
$this->tank_auth->get_username()
( $this->ci->session->userdata('username')
,调用$this->ci->session->userdata('username')
) 。 This can then be stored and passed to a view. 然后可以将其存储并传递到视图。
In Tank-Auth you will see: 在Tank-Auth中,您将看到:
application/controller/welcome.php 应用/控制器/的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 应用/视图/的welcome.php
Hi, <strong><?php echo $username; ?></strong>! You are logged in now.
ok when user is logged in you need to store his or her data to session . 好的,当用户登录后,您需要存储他或她的数据进行会话。 when you verify user get his or her data and store that in session.
当您验证用户获取他或她的数据并将其存储在会话中时。
$data = array(
'username' => 'johndoe',
'email' => 'johndoe@some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata('logged_info', $data );
and then you can get it any where like 然后你可以在任何地方得到它
$userdata = $this->session->userdata('logged_info');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.