简体   繁体   English

Codeigniter:以管理员用户身份登录并以员工用户身份登录

[英]Codeigniter: login as admin user and login as employee user

Hi I'm currently working on codeigniter framework and I'm currently working on a project which is basically an admin/employee login system. 嗨,我目前正在开发codeigniter framework ,目前正在从事一个基本上是管理员/员工登录系统的项目。

Currently I have been able to create a login form, and can successfully get a user to login, 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 an employee user to an employees 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 to login. 但是允许任何用户登录。

By the way I have only one table name "employees" which consist only for employee user and admin user from mysql database. 顺便说一句,我只有一个表名“ employees”,仅由mysql数据库中的雇员用户和admin用户组成。

Here is the view: (login.php) 这是视图:(login.php)

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="viewport" content="width=1,initial-scale=1,user-scalable=1" />
  <title> Login Form </title>

  <link href="http://fonts.googleapis.com/css?family=Lato:100italic,100,300italic,300,400italic,400,700italic,700,900italic,900" rel="stylesheet" type="text/css">
  <link rel="stylesheet" type="text/css" href="<?php echo base_url()?>assets/css/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" href="<?php echo base_url()?>assets/css/styles.css" />
  <script src="<?php echo base_url()?>assets/js/bootstrap.min.js"></script>

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body>

<script>
$(document).ready(function () {
  var elements = document.getElementsByTagName("INPUT");
  for (var i = 0; i < elements.length; i++) {
    elements[i].oninvalid = function (e) {
      e.target.setCustomValidity("");
      if (!e.target.validity.valid) {
        switch (e.srcElement.id) {
          case "username":
          e.target.setCustomValidity("Username cannot be blank");
          break;
          case "pass":
          e.target.setCustomValidity("Password cannot be blank");
          break;
        }
      }
    };
    elements[i].oninput = function (e) {
      e.target.setCustomValidity("");
    };
  }
})
</script>

<style type="text/css">
.back{
  position: absolute;
  bottom: 0px;
  left: 0px;
}
</style>

<section class="container">
      <section class="login-form">
    <form class="form-signin" action="goClock" method="post" role="login">
               <img src="<?php echo base_url()?>assets/images/easypay.png" class="img-responsive center-block" alt="" />
        <input type="text" class="form-control input-lg" placeholder="username" name='user' required id="username" autocomplete="off" autofocus  />
        <input type="password" class="form-control input-lg" placeholder="Password" name='password' required id="pass" required />
       <?php

            if(!empty($login_error)) {
              echo $login_error;
            }

       ?>  
            <button name='login' class="btn btn-lg btn-block btn-primary" value=" LOGIN " type="submit" id="button2">Login</button>  
</form>
        <p class="text-center" style="font-weight: bold; font-size: 60;" id="demo"></p>

            <script>
            var myVar = setInterval(myTimer, 1000);

            function myTimer() {
              var d = new Date();
              document.getElementById("demo").innerHTML = d.toLocaleTimeString();
            }
            </script>
</section>
</section>
<div class="back">
  <input class="btn btn-warning" action="action" type="button" value="<- Back" onclick="location.href = 'index';" />
</div>
</body>
</html>

Here is the controller: (home.php) 这是控制器:(home.php)

   public function goClock($message = null) {
            $this->load->view('imports/header');
            $this->load->view('imports/menu');

            if (!is_null($this->input->post('login'))) {
                $username = $this->input->post('user');
                $password = $this->input->post('password');

                $userdata = $this->model_home->get_userinfo($username, $password);

                $_SESSION['is_loggedin'] = true;  

                if ($userdata !== false) {
                    $this->session->set_userdata($userdata);
                    redirect('home/goHome');
                }else {
                    $data['login_error'] = '<p style="font-size: 16px; color: red;" align="center">Invalid Username or Password</p>';
                    $this->load->view('login', $data);
                }
            }else {
                $data['username'] = $this->input->post('user');
                $data['password'] = $this->input->post('password');
                $this->load->view('clock', $data);
            }
        }

And finally here is the model: (model_home.php) 最后是模型:(model_home.php)

public function get_userinfo($username = null, $password = null) {
        if ($username && $password) {
            $this->db->select('username, password, empnum');
            $this->db->where('username', $username);
            $this->db->where('password', $password);
            $query = $this->db->get('employees');
            if ($query->num_rows()) {
                return $query->result_array()[0];
            }
        }

        return false;
    }

Just to clarify I have only one controller, my main controller by default which is home.php. 为了澄清我只有一个控制器,默认情况下我的主控制器是home.php。 In conclusion my aim is to be able to redirect an admin user to admin.php and to be able to redirect a employee user to employee.php (Hasn't been created yet). 总之,我的目标是能够将admin用户重定向到admin.php,并且能够将员工用户重定向到employee.php(尚未创建)。

PS I already have a column in my table in PhpMyAdmin called "employees". PS:我在PhpMyAdmin的表中已经有一个称为“员工”的列。

Please help me. 请帮我。 Thank you in advance. 先感谢您。

you should add one extra column in Database named as user_type and Set default value=0 for that and for "admin" set it 1. Now modify your query like this: 您应该在数据库中添加一个名为user_type的额外列,并为此设置默认值= 0,并为“ admin”设置它的默认值1。现在像这样修改查询:

if ($username && $password) {
            $this->db->select('username, password, empnum,user_type');
            $this->db->where('username', $username);
            $this->db->where('password', $password);
            $query = $this->db->get('employees');
            if ($query->num_rows()) {
                return $query->result_array()[0];
            }
        }

Now check the $userdata[] array value having user_type key if it is 1 then 现在检查具有user_type键的$ userdata []数组值是否为1,然后

$this->load->view('admin', $data);

else he is user_type = 0 which means employee $this->load->view('employee', $data); 否则他是user_type = 0,这意味着员工$ this-> load-> view('employee',$ data);

NOTE:I can provide the way but here you have to modify your array key and view name as per the need. 注意:我可以提供方法,但是在这里您必须根据需要修改阵列键和视图名称。

First create a filed with name usertype in your database employees table. 首先在数据库employees表中创建一个名称为usertype的文件。

Run this query in your phpMyadmin database 在您的phpMyadmin数据库中运行此查询

  ALTER TABLE `employees` ADD `usertype` ENUM('user','employee') NOT NULL DEFAULT 'user' ;

and add following code on your Model 并在Model上添加以下代码

 public function get_userinfo($username = null, $password = null) {
    if ($username && $password) {
        $this->db->select('username, password, empnum,usertype');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('employees');
        if ($query->num_rows()) {
            return $query->row();
        }
    }

    return false;
}

And on your controller 在您的控制器上

public function goClock($message = null) {
        $this->load->view('imports/header');
        $this->load->view('imports/menu');

        if (!is_null($this->input->post('login'))) {
            $username = $this->input->post('user');
            $password = $this->input->post('password');

            $userdata = $this->model_home->get_userinfo($username, $password);

            $_SESSION['is_loggedin'] = true;  

            if ($userdata !== false) {
               if($userdata->usertype=='user'){ # if user then redirect to user page

                  redirect('home/goHome');
               }else{
                  # If employee redirect to admin page
               }
            }else {
                $data['login_error'] = '<p style="font-size: 16px; color: red;" align="center">Invalid Username or Password</p>';
                $this->load->view('login', $data);
            }
        }else {
            $data['username'] = $this->input->post('user');
            $data['password'] = $this->input->post('password');
            $this->load->view('clock', $data);
        }
    }

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

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