繁体   English   中英

CodeIgniter用户/管理员登录

[英]CodeIgniter User/Admin Login

嗨,我是CodeIgniter的新手,目前正在从事一个基本上是管理员/用户系统的项目。

目前,我已经能够创建登录表单,并且在检查用户存在之后可以成功让用户登录。

我只是想为正确的方向指明方向,以便能够将普通用户登录到成员页面,将管理员用户登录到管理页面。

这就是我目前所拥有的,它可以使用引导程序作为前端框架来充分发挥作用。 但是允许我的成员表中的任何用户登录。

视图:

<?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>

这是我的登录控制器(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');
               }
          }

以下是控制器内指向成员页面(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');
        }
    }

最后是我的模型(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();
     }
}

为了清楚起见,我有两个控制器,默认情况下,我的主控制器是site.php,而登录控制器名为login.php。 总之,我的目标是能够将管理员用户重定向到members.php,并能够将普通用户重定向到agent.php(尚未创建)。

PS我在PhpMyAdmin的表中已经有一个称为“ admin”的列,我只使用“是”或“否”来标识它们是否存在。

这是一个来自我的项目的示例:

        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
        }

对于您而言,您不能这样做,因为您的模型不会返回用户,而是返回一个num行。 只是改变

return $query->num_rows();

通过

return $query->row();

然后,您应该能够测试您的用户是否是管理员。

暂无
暂无

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

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