简体   繁体   中英

CodeIgniter User/Admin Login

Hi I'm fairly new to CodeIgniter and am currently working on a project which is basically an admin/user system.

Currently I have been able to create a login form, and can successfully get a user to sign in, after having checked the user exists.

I just wanted to be pointed in the right direction as to how I could go about this to be able to login a normal user to a members page, and an admin user to an admin page.

This is what I have at the moment, which is fully functioning using bootstrap as a front end framework. But allows any user from my members table to login.

View:

<?php $attributes = array("class" => "form-horizontal", "id" => "loginform", "name" => "loginform");
                echo form_open("login/loginchk", $attributes);?>
                <h2>Please Login</h2>
                <hr>
                <fieldset>
               <!--<legend>Login</legend>-->
               <div class="form-group">
               <div class="row colbox">
               <div class="col-lg-4 col-sm-4">
                    <label for="txt_username" class="control-label">Username</label>
               </div>
               <div class="col-lg-8 col-sm-8">
                    <input class="form-control" id="txt_username" name="txt_username" placeholder="Username" type="text" value="<?php echo set_value('txt_username'); ?>" />
                    <span class="text-danger"><?php echo form_error('txt_username'); ?></span>
               </div>
               </div>
               </div>

               <div class="form-group">
               <div class="row colbox">
               <div class="col-lg-4 col-sm-4">
               <label for="txt_password" class="control-label">Password</label>
               </div>
               <div class="col-lg-8 col-sm-8">
                    <input class="form-control" id="txt_password" name="txt_password" placeholder="Password" type="password" value="<?php echo set_value('txt_password'); ?>" />
                    <span class="text-danger"><?php echo form_error('txt_password'); ?></span>
               </div>
               </div>
               </div>

               <div class="form-group">
               <div class="col-lg-12 col-sm-12 text-center">
                    <input id="btn_login" name="btn_login" type="submit" class="btn btn-default" value="Login" />
                    <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-default" value="Cancel" />
               </div>

Here is my login controller (login.php):

public function loginchk()
     {
          //get the posted values
          $username = $this->input->post("txt_username");
          $password = $this->input->post("txt_password");

          //set validations
          $this->form_validation->set_rules("txt_username", "Username", "trim|required");
          $this->form_validation->set_rules("txt_password", "Password", "trim|required");

          if ($this->form_validation->run() == FALSE)
          {
               $data["title"] ="SmartAgent";
               //validation fails
               $this->load->view("site_header");
               $this->load->view("content_home", $data);
               $this->load->view("site_footer");
          }
          else
          {
               //validation succeeds
               if ($this->input->post('btn_login') == "Login")
               {
                    //check if username and password is correct
                    $usr_result = $this->login_model->get_user($username, $password);
                    if ($usr_result > 0) //active user record is present
                    {
                         //set the session variables
                         $sessiondata = array(
                              'username' => $username,
                              'loginuser' => TRUE
                         );
                         $this->session->set_userdata($sessiondata);
                         redirect("site/members");
                    }
                    else
                    {
                         $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username and password!</div>');
                         redirect('site/home');
                    }
               }
               else
               {
                    redirect('site/home');
               }
          }

Here is the code within the controller which directs to the members page (site.php):

public function members(){
            $data["title"] ="Admin Panel";
            $this->load->model("notifications");

            if ($this->session->userdata('loginuser')){
            $this->load->view("site_header");
            $this->load->view("site_nav");
            $this->load->view('members', $data);
            $this->load->view("site_footer");
        } else {
            redirect('site/restricted');
        }
    }

And Finally here is my model (login_model.php):

     function get_user($usr, $pwd)
     {
          $this->db->where('username', $usr);
          $this->db->where('password', md5($pwd));
          $query = $this->db->get('members');
          //echo $query;
          return $query->num_rows();
     }
}

Just to clarify I have two controllers, my main controller by default which is site.php and the login controller called login.php. In conclusion my aim is to be able to redirect an admin user to members.php and to be able to redirect a normal user to agent.php (Hasn't been created yet).

PS I already have a column in my table in PhpMyAdmin called "admin" and I simply use 'Yes' or 'No', to identify if they are or not.

Here is an example taken from one of my projects :

        if ($this->form_validation->run())
        {
            $user   = $this->m_user->get_user_by_mail($this->input->post("login_mail"));
            //Set session
            $this->session->set_userdata(array('user' => $user));


            if($user->is_admin == 1)
                redirect('BackOffice');
            else
                redirect('FrontOffice');
        }
        else
        {
            $data["view"] = "home";
            $this->layout->load_template($data); //This function do stuff an load a view
        }

In your case, you can't do that because your model doesn't return you an user but a num row. Just change

return $query->num_rows();

By

return $query->row();

And then you should be able to test if your user is an admin or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM