php CodeIgniter 用户/管理员登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28387549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:50:19  来源:igfitidea点击:

CodeIgniter User/Admin Login

phpcodeigniterlogin

提问by Abdush Samad Miah

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. 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).

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

P.S 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”,我只是使用“是”或“否”来确定它们是否存在。

回答by AdrienXL

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

在您的情况下,您不能这样做,因为您的模型不会返回用户,而是返回 num 行。只是改变

return $query->num_rows();

By

经过

return $query->row();

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

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