Check if admin has logged in (CodeIgniter)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ethio
    New Member
    • Dec 2012
    • 2

    Check if admin has logged in (CodeIgniter)

    If there's someone logged in, this code checks the sessions and I can successfully view the page, but now I'd like to check if the user is admin. I've tried checking for this in the model and I keep getting a few errors. One of them is "Fatal error: Call to undefined function where() in C:\xampp\htdocs \ecwm604\applic ation\models\us ermodel.php on line 54"

    Code:
    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');
    		}
    	}
    The line $this->admin($usernam e,$password); passes to admin() below
    Code:
    --Controller Auth 
    
        public function authenticate()
    	{
        $username = $this->input->post('uname');
        $password = $this->input->post('pword');
        $user = $this->authlib->login($username,$password);
        >> $this->admin($username,$password); <<passes to admin()below
        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;
    	}
    	}
    Code:
    --Library Authlib 
    
        public function adminlib($user,$pwd) 
    	{
    	return $this->ci->usermodel->chkadmn($user,$pwd);
    	}
    Error is taking place on line 5 below:
    Code:
    --Model Usermodel 
    
        function chkadmn($username,$password)
    	{
    <<This is where the error is taking place>>$this->db-where(array('username' => $username,'password' => sha1($password)));
    	$res = $this->db->get('users',array('type'));
    	if ($res->num_rows() != 1) {
    	return false;
    	}
    	}
    Last edited by acoder; Dec 31 '12, 06:24 PM. Reason: Added comments from code
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    ethio:

    Merely posting the code and giving a generic, paraphrased, error message and the "this doesn't work" is not enough to help you. Bytes is not a code writing nor homework service. Please read the FAQ and posting guidlines. I hate to just outright delete the post as you've taken the time to include your code.

    However:

    We must insist that you take the time to do the basic trouble shooting on your own system first in that we do not have the same PC, programs, nor access to your data.

    As for errors, you must provide the EXACT title, error number, and error description along with at what point in your code the error occurs - without this basic information, it's is difficult at best to offer any help.

    So, without some basic effort and information on your part, even if someone manages to offer you some guidance, your thread may be subject to deletion.

    Comment

    • ethio
      New Member
      • Dec 2012
      • 2

      #3
      I have taken the time to do basic trouble shooting on my own system. In fact, I've been trying to fix this since yesterday night. But I have added the exact error message and it happens in the usermodel $this->db->where(...

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        I've added some comments to your post (taken from your code) which should help explain the problem better (it's easy to miss in all the lines of code).

        I don't use CodeIgniter, but is there not a better method to check for admin, e.g. roles?

        As for your error, is $this->db-where a typo or did you miss the > character, i.e. it should be $this->db->where?

        Comment

        Working...