Codeigniter 不显示以用户名记录的当前会话

我想显示当前用户的姓名。


在视图中 (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');

       

    }

}


青春有我
浏览 91回答 3
3回答

噜噜哒

您的方法有几个问题,您正在打印用户名$username = $this->session->userdata('logged_in');echo $username;&nbsp;但是您没有在任何地方分配用户名,因此要完成此操作,您可以在登录类中进行一些选择class UserLogin extends CI_Model{&nbsp; public function checkLogin($email, $password) {&nbsp; &nbsp; $this->db->where('email', $email);&nbsp; &nbsp; $this->db->where('password', $password);&nbsp; &nbsp; $query = $this->db->get('users');&nbsp; &nbsp; &nbsp; if($query->num_rows())&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $user = $query->row_array();&nbsp; &nbsp; &nbsp; &nbsp; print_r($user); die(); // remove die after testing&nbsp; &nbsp; &nbsp; &nbsp; return $user;&nbsp; &nbsp; &nbsp; }&nbsp; return false;}比你能回声:$user = $this->session->userdata('logged_in');echo $user['username'];第二个问题是这种方式登录用户非常不安全,您可以对密码进行md5或其他加密。IE:$this->db->where('password', md5( $password));或更好的用户ion_auth library,默认情况下为您提供各种选项。

ibeautiful

更改以下CheckLogin()类似然后检查用户数据public function checkLogin($email, $password) {&nbsp; &nbsp; $this->db->where('email', $email);&nbsp; &nbsp; $this->db->where('password', $password);&nbsp; &nbsp; $query = $this->db->get('users');&nbsp; &nbsp; if($query->num_rows() == '1'){&nbsp; &nbsp; &nbsp; &nbsp; $data = $query->row_array();&nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_userdata(array('name'=>$data['name'],'loggedin'=>'1'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; return $query->num_rows();

米琪卡哇伊

请尝试更改模型class UserLogin extends CI_Model{&nbsp; &nbsp; &nbsp;public function checkLogin($email, $password) {&nbsp; &nbsp; $this->db->where('email', $email);&nbsp; &nbsp; $this->db->where('password', $password);&nbsp; &nbsp; return $this->db->get('users');}
打开App,查看更多内容
随时随地看视频慕课网APP