Codeigniter登录系统问题记录 [英] Codeigniter login system issue loging

查看:72
本文介绍了Codeigniter登录系统问题记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表"usuarios",其中包含以下信息:

I got a table "usuarios" which contains the following information:

(ID,用户名,名称,姓氏,密码,类型,状态,日期),对吗?

(id, username, name, lastname, password, type, status, date), right?

看看:

我的个人目标是建立一个登录系统,使我可以从那些只是简单用户的用户中区分那些谁是ADMIN ...我的意思是,如果表中的一行包含type = 0,则它是ADMIN,如果type = 1,这是一个简单的用户. 因此,在登录系统中,如果我编写例如ADMIN 1234,并且此数据对应于"users"表中的ADMIN用户;当我按下登录"按钮时,它必须仅为ADMIN打开一个独占页面..但对于简单"用户则相同.

My personal goal is to make a login system which allows me to clasify those WHO are ADMIN from those who are just simple users... I mean, if a row of the table contains type = 0 it is an ADMIN and if type = 1 it is a simple user. So,in the login system if i write for example ADMIN 1234 and this data corresponds to an ADMIN user from the table "users"; when i press "login" it must open an exclusive page only for ADMINs.. The same but for those who are "simple" users.

系统必须验证我是否输入了错误的用户名/密码,以及用户名是否对应于活动成员(状态= 1活动,状态= 0非活动).登录时没有空字段

The system must validate if i write wrongs usernames/passwords and if the username corresponds to an active member (status = 1 active, status = 0 not active). No empty fields while login

无论我在登录系统中的用户名"和密码"中写什么..当我单击登录"时,我总是收到此消息,但没有任何反应:

这些是在我的数据库中注册的用户:

And these are the users registered in my database:

这是我的代码: 控制器文件(2个文件:仪表板"和登录") 仪表板:

Here is my code: Controller files (2 files: "dashboard" and "login") Dashboard:

    <?php

        Class dashboard extends CI_Controller{

         public function __construct(){
             parent::__construct();
             $this->load->model('m_login');
         $this->auth->cek_auth();

        }

        public function index(){
            $ambil_akun = $this->m_login->ambil_user($this->session->userdata('usuario'));
            $data = array(

              'user' =>$ambil_akun,

              );

            $stat = $this->session->userdata('lvl');
            if($stat=='s_admin'){
              $this->load->view('dashboard_admin', $data);

            }else{

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

            }

        }

  public function login(){

    $session = $this->session->userdata('isLogin');
    if($session == FALSE)
    {

      $this->load->view('login_form');

    }else
    {

      redirect('dashboard');

    }

  }

  public function logout(){

    $this->session->sess_destroy();
    redirect('login', 'refresh');

  }

}

?>

登录:

    <?php

        Class Login extends CI_Controller{

        public function __construct(){
             parent::__construct();
             $this->load->model("m_login");

        }


        public function index(){
            $session = $this->session->userdata('isLogin');
            if($session == FALSE)
            {

                $this->load->view('login_form');

            }else
            {

                redirect('dashboard');

            }

        }

        public function do_login()
        {

            $usuario=$this->input->post('uname');
            $contrasena=$this->input->post('pass');

            $cek=$this->m_login->cek_user($contrasena, md5($contrasena));
            if(count($cek) == 1){

                foreach ($cek as $cek) {

                    $tipo=$cek['tipo'];
                    $estado=$cek['estado'];
                    $nombre=$cek['nombre'];


                }


                if($estado =='0'){

                    $this->session->set_userdata(array(

                        'isLogin'=>TRUE,
                        'uname'=>$usuario,
                        'lvl'=>$tipo,
                        'estado'=>$estado

                    ));
                    redirect('dashboard', 'refresh');

                }
                else{

                    echo "<script>alert('YOUR ACCOUNT IS NOT ACTIVE')</script>";
                    redirect('login','refresh');

                }

                }else{

                    echo "<script>alert('USERNAME OR PASSWORD INVALID!')</script>";
                    redirect('login','refresh');

                }



}

}

?>

模型文件(m_login):

The model file (m_login):

    <?php

    class m_login extends CI_Model{

        public function __construct(){

         parent::__construct();
         $this->tbl="usuarios";

        }

    public function cek_user($usuario="", $contrasena="")
    {


        $query=$this->db->get_where($this->tbl, array('usuario'=>$usuario, 'contrasena'=>$contrasena));
        $query=$query->result_array();
        return $query;
    }


    public function ambil_user($nombre)
    {

        $query=$this->db->get_where($this->tbl, array('nombre'=> $nombre));
        $query=$query->result_array();
        if(query){

            return $query[0];

        }

    }


}

?>

我的视图文件: login_form:

And my view files: login_form:

    <!DOCTYPE html>
<html lang="">

    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>


    </head>

        <body>
            <div class="container">
            <h3>Login</h3>
            <hr>
            <form action="<?php echo base_url('login/do_login')?>" method="POST">
                <div class="form-group">
                    <label for="cari">USERNAME</label>
                    <input type="text" name="usuario" id="usuario" class="form-control"><?php echo form_error('usuario');?>

                </div>

                <div class="form-group">
                    <label for="cari">PASSWORD</label>
                    <input type="password" name="contrasena" id="contrasena" class="form-control"><?php echo form_error('contrasena');?>

                </div>

                <input class="btn btn-primary" type="submit" value="Login">
                <input class="btn btn-primary" type="reset" value="Reset">

            </form>
                </div>



        </body>
</html>

dashboard_admin:

dashboard_admin:

Hi!<b><?php echo $user['nombre'];?></b>YOU ARE AN ADMINISTRATOR! > [<a href= '<?php echo base_url('dashboard/logout');?>'>LOGOUT</a>]

dashboard_user:

dashboard_user:

Hi!<b><?php echo $user['nombre'];?></b>YOU ARE A SIMPLE USER! > [<a href= '<?php echo base_url('dashboard/logout');?>'>LOGOUT</a>]

不知道现在该怎么做:/

Dont know what to do now :/

推荐答案

在此代码中,您对密码进行了加密.但是在db上,它没有用md5加密吗? 也许不使用md5发送就可以解决您的问题.但我认为也可以使用md5的良好选择进行加密.

in this code, you encrypt password. But on db, its not encrypted with md5 ? Maybe sending without md5 will solve your problem for now. But i think also encrypt with md5 good selection.

$cek=$this->m_login->cek_user($contrasena, $contrasena); // change like this.

这篇关于Codeigniter登录系统问题记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆