我想显示当前用户的姓名。
在视图中 (dashboard.php)
<?php
$username = $this->session->userdata('logged_in');
echo $username;
?>
我的数据库在表“用户”中有一个字段“名称”。用户名保存在这里。但我得到的输出只是“名称”
所以,我在下面添加了这个
<?php
$username = $this->session->userdata('logged_in');
echo $username;
print_r($this->session->all_userdata());
?>
输出是
名称数组([__ci_last_regenerate] => 1583656504 [name] => name [] => [logged_in] => 1)
问题在哪里?
我注册了新用户,值已正确保存在数据库中,我也可以登录。但无法显示登录用户名。
Login.php(控制器)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct() {
parent::__construct();
// Load url helper
$this->load->helper('url');
$this->load->library(['form_validation','session']);
$this->load->database();
//load the Login Model
$this->load->model('UserLogin', 'login');
}
public function index()
{
$logged_in = $this->session->userdata('logged_in');
if($logged_in){
//if yes redirect to welcome page
redirect(base_url().'app/dashboard');
}
$data['title'] = 'Login';
$this->load->view('app/login', $data);
}
public function doLogin() {
//get the input fields from login form
$name = $this->input->post('name');
$email = $this->input->post('email');
$password = sha1($this->input->post('password'));
//send the email pass to query if the user is present or not
$check_login = $this->login->checkLogin($email, $password);
//if the result is query result is 1 then valid user
if ($check_login) {
// $this->session->set_userdata('logged_in', true);
$this->session->set_userdata('logged_in', $check_login);
redirect(base_url().'app/dashboard');
}
}
噜噜哒
ibeautiful
米琪卡哇伊