0

如果有人登录,这段代码会检查会话,我可以成功查看页面,但现在我想检查用户是否是管理员。我尝试通过以下方式检查模型,这是我尝试过的,但它不起作用。

检查会话以及是否为管理员的方法

public function index()
{
    $this->load->library('authlib');
    $loggedin = $this->authlib->is_loggedin();
    ///$admin = $this->auth->admin();

    if ($loggedin === false) {
    $this->load->helper('url');
    redirect('/auth/login');
    }
    if ($this->auth->admin() === false) {
    $message ['msg'] = "You are not an admin!";
    $this->load->view('homeview', $message);
    }
    else
    {
    $this->load->view('add_view');
    }
}

认证控制器

public function authenticate()
{
$username = $this->input->post('uname');
$password = $this->input->post('pword');
$user = $this->authlib->login($username,$password);
**>> $this->admin($username,$password); << passes the posted in values**
if ($user !== false) {
    $this->load->view('homeview',array('name' => $user['name']));

}
else {
    $data['errmsg'] = 'Unable to login - please try again';
    $this->load->view('login_view',$data);
}   
}

public function admin($username,$password){
//$this->load-model('usermodel');
$admin = $this->authlib->adminlib($username,$password);
if ($admin == false){
return false;
//if ($res->num_rows() != 1){
//return false;
}
}

库 authlib

public function adminlib($user,$pwd) 
{
return $this->ci->usermodel->chkadmn($user,$pwd);
}

该模型

function chkadmn($username,$password)
{
$this->db-where(array('username' => $username,'password' => sha1($password)));
$res = $this->db->get('users',array('type'));
if ($res->num_rows() != 1) {
return false;
}
}

做了一些更改,现在我得到“在第 54 行调用 C:\xampp\htdocs\ecwm604\application\models\usermodel.php 中的未定义函数 where()”

4

1 回答 1

0

就个人而言,我将替换您的所有 if 语句,并按如下所示对其进行测试:

  public function index()
    {
        $this->load->library('authlib');
        $loggedin = $this->authlib->is_loggedin();
        ///$admin = $this->auth->admin();

        if (!$loggedin) {
        $this->load->helper('url');
        redirect('/auth/login');
        }

        if (!$this->auth->admin()) {
        $message ['msg'] = "You are not an admin!";
        $this->load->view('homeview', $message);
        }
        else
        {
        $this->load->view('add_view');
        }

    }

    public function admin(){
    $this->load-model('usermodel');
    $admin = $this->usermodel->chkadmn($username,$password);
    if (!$admin){
    return false;
    //if ($res->num_rows() !== 1){
    //return false;
    }
    }

    function chkadmn($username,$password)
    {
    $this->db-where(array('username' => $username,'password' => sha1($password)));
    $res = $this->db->get('users',array('type'));
    if ($res->num_rows() !== 1) {
    return false;
    }
    }
于 2012-12-31T14:33:21.330 回答