自定义codeigniter库不返回数据

时间:2015-07-04 13:43:04

标签: codeigniter

我已经创建了一个codeigniter用户登录库但遇到了一个小问题。当我登录时,它不会返回库中的任何功能,如$this->get_user_username();

在我的信息中心上,我应该可以执行此回显$ this-> user-> get_user_username();

  

我想我知道问题是因为if($ this-> validate_password($ password)){在查询周围。然后停下来   我返回任何数据。

注意:我只希望能够在有效的会话中设置user_id。并将其他人作为变量等返回。

问题因为我在登录时使用if ($this->validate_password($password)) {并且登录后阻止我检索任何数据什么是最佳解决方案所以仍然可以使用它并检索我的数据

  

如我所说的那样在会话中设置所有内容我只需要设置用户ID   在会议中。

信息中心控制器

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends MX_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('admin/user');
    }

    public function index()
    {
        echo $this->user->get_user_username();
        print_r($_SESSION);
        $data['top_navbar'] = Modules::run('admin/common/top_navbar/index');
        $data['column_left'] = Modules::run('admin/common/column_left/index');
        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('common/dashboard_view', $data);
    }
}

用户库

<?php

class User {

    private $user_id;
    private $username;

    public function __construct() {
        $this->CI =& get_instance();
    }

    public function login($username, $password) {
        if ($this->validate_password($password)) {

            $this->CI->db->where('username', $username);
            $user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');

            if ($user_query->num_rows() > 0) {

                $row = $user_query->row();

                $this->CI->session->set_userdata(array('user_id' => $row->user_id)); // Working

                $this->user_id = $row->user_id;
                $this->user_group_id = $row->user_group_id;
                $this->username = $row->username;
                $this->firstname = $row->firstname;
                $this->lastname = $row->lastname;
                $this->email = $row->email;

                return true;

            } else {

                return false;   

            }

        } else {

            return false;
        }

    }

    public function logout() {
        $this->CI->session->unset_userdata('user_id');

        $this->user_id = '';
        $this->username = '';
    }

    public function get_user_id() {
        return $this->user_id;
    }

    public function get_user_group_id() {
        return $this->user_group_id;
    }

    public function get_user_username() {
        return $this->username;
    }

    public function get_user_firstname() {
        return $this->firstname;
    }

    public function get_user_lastname() {
        return $this->lastname;
    }

    public function get_user_email() {
        return $this->email;
    }

    public function validate_password($password) {
        if (password_verify($password, $this->stored_hash())) {
            return $password;
        } else {
            return false;
        }
    }

    public function stored_hash() {
        $this->CI->db->where('username', $this->CI->input->post('username')); 
        $query = $this->CI->db->get($this->CI->db->dbprefix . 'user');

        if ($query->num_rows() > 0) {
            $row = $query->row();
            return $row->password;
        } else {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

解决问题我想发布自己的答案,我不得不在用户库的构造区域中添加另一个部分以使其工作

然后使用session userdata进行检查。

public function __construct() {
    $this->CI =& get_instance();

    if ($this->CI->session->userdata('user_id') == TRUE) {

        $this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
        $user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');

        if ($user_query->num_rows() > 0) {
            $row = $user_query->row();
            $this->user_id = $row->user_id;
            $this->user_group_id = $row->user_group_id;
            $this->username = $row->username;
            $this->firstname = $row->firstname;
            $this->lastname = $row->lastname;
            $this->email = $row->email;

            return true;
        } else {
            return false;
        }
    }
}

<?php

class User {

    private $user_id;
    private $username;

    public function __construct() {
        $this->CI =& get_instance();

        if ($this->CI->session->userdata('user_id') == TRUE) {

            $this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
            $user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');

            if ($user_query->num_rows() > 0) {

                $row = $user_query->row();

                $this->user_id = $row->user_id;
                $this->user_group_id = $row->user_group_id;
                $this->username = $row->username;
                $this->firstname = $row->firstname;
                $this->lastname = $row->lastname;
                $this->email = $row->email;

                return true;

            } else {

                return false;
            }

        } else {

            $this->logout();
        }
    }

    public function login($username, $password) {
        if ($this->validate_password($password)) {

            $this->CI->db->where('username', $username);
            $user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');

            if ($user_query->num_rows() > 0) {

                $row = $user_query->row();

                $this->CI->session->set_userdata(array('user_id' => $row->user_id));

                $this->user_id = $row->user_id;
                $this->user_group_id = $row->user_group_id;
                $this->username = $row->username;
                $this->firstname = $row->firstname;
                $this->lastname = $row->lastname;
                $this->email = $row->email;

                return true;

            } else {

                return false;   

            }

        } else {

            return false;
        }

    }

    public function logout() {
        $this->CI->session->unset_userdata('user_id');

        $this->user_id = '';
        $this->username = '';
    }

    public function get_user_id() {
        return $this->user_id;
    }

    public function get_user_group_id() {
        return $this->user_group_id;
    }

    public function get_user_username() {
        return $this->username;
    }

    public function get_user_firstname() {
        return $this->firstname;
    }

    public function get_user_lastname() {
        return $this->lastname;
    }

    public function get_user_email() {
        return $this->email;
    }

    public function validate_password($password) {
        if (password_verify($password, $this->stored_hash())) {
            return $password;
        } else {
            return false;
        }
    }

    public function stored_hash() {
        $this->CI->db->where('username', $this->CI->input->post('username')); 
        $query = $this->CI->db->get($this->CI->db->dbprefix . 'user');

        if ($query->num_rows() > 0) {
            $row = $query->row();
            return $row->password;
        } else {
            return false;
        }
    }
}