简体   繁体   English

CodeIgniter用户/管理员登录

[英]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. 嗨,我是CodeIgniter的新手,目前正在从事一个基本上是管理员/用户系统的项目。

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): 这是我的登录控制器(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): 以下是控制器内指向成员页面(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): 最后是我的模型(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. 为了清楚起见,我有两个控制器,默认情况下,我的主控制器是site.php,而登录控制器名为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). 总之,我的目标是能够将管理员用户重定向到members.php,并能够将普通用户重定向到agent.php(尚未创建)。

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. PS我在PhpMyAdmin的表中已经有一个称为“ admin”的列,我只使用“是”或“否”来标识它们是否存在。

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. 对于您而言,您不能这样做,因为您的模型不会返回用户,而是返回一个num行。 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. 然后,您应该能够测试您的用户是否是管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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