无法登录管理信息中心

时间:2017-12-19 17:14:32

标签: codeigniter authentication login

我的密码和用户名是正确的,但我无法登录管理员。我的控制器有什么问题吗?

这是我的模特:

class login_model extends CI_Model{
   function cek($username, $password){
       $this->db->where("username", $username);
       $this->db->where("password", $password);
       return $this->db->get("user_admin");
   }
   function getLoginData($usr, $psw){
       $u = $usr;
       $p = md5($psw);
       $q_cek_login = $this->db->get_where('user_admin', array('username' => $u, 'password' => $p));
       if(count($q_cek_login->result()) > 0){
           foreach($q_cek_login->result() as $qck){
               foreach($q_cek_login->result() as $qad){
                   $sess_data['logged_in'] = TRUE;
                   $sess_data['id'] = $qad->id;
                   $sess_data['username'] = $qad->username;
                   $sess_data['password'] = $qad->password;
                   $sess_data['email'] = $qad->email;
                   $sess_data['level'] = $qad->level;
                   $this->session->set_userdata($sess_data);
               }
               redirect('welcome_message');
           }
       }else{
           $this->session->set_flashdata('result_login'. 'username dan password salah');
           header('location: '. base_url(). 'login');
       }
   }
}

这是我的控制者:

class login extends CI_Controller {
function _construct(){
    parent::_construct();
    if($this->session->userdata('username')){
        redirect(base_url('welcome_message'));
    }
    $this->load->model(array('login_model'));
}
function index(){
    $this->load->view('login');
}

function proses(){
    $this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
    $this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');
    if($this->form_validation->run() == FALSE){
        $this->load->view('login');
    }else{
        $usr = $this->input->post('username');
        $psw = $this->input->post('password');
        $u = $usr;
        $p = md5($psw);
        $cek = $this->login_model->cek($u, $p);
        if($cek->num_rows() > 0 ){
            foreach($cek->result() as $qad){
                $sess_data['id'] = $qad->id;
                $sess_data['email'] = $qad->email;
                $sess_data['username'] = $qad->username;
                $sess_data['level']=$qad->level;
                $this->session->set_userdata($sess_data);
            }
            $this->session->set_flashdata('success', 'login berhasil');
            redirect(base_url('/'));
        }else{
            $this->session->set_flashdata('result_login', 'username dan password yang anda masukkan salah');
            redirect(base_url('login'));
        }
    }
}

我的观点:

<div class="login-box-body">
<p class="login-box-msg">Sign in to start your session</p>
<form action="<?php echo base_url('login/proses'); ?>" method="post">
<?php if (validation_errors() || $this->session->flashdata('result_login')) { ?> 
        <div class="alert alert-danger animated fadeInDown" role="alert">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <strong>Peringatan!</strong>
            <?php echo validation_errors(); ?>
            <?php echo $this->session->flashdata('result_login'); ?>
        </div>
  <?php } ?>
  <div class="form-group has-feedback">
    <input type="text" class="form-control" placeholder="Username" id="username" name="username">
    <span class="glyphicon glyphicon-user form-control-feedback"></span>
  </div>
  <div class="form-group has-feedback">
    <input type="password" class="form-control" placeholder="Password" id="password" name="password">
    <span class="glyphicon glyphicon-lock form-control-feedback"></span>
  </div>
  <div class="row">
    <div class="col-xs-8">
    <a href="http://localhost/adminpmjb/register" class="text-center">Register a new admin</a>
    </div>

当我尝试登录时,我总是收到此错误:

Screenshot of error

1 个答案:

答案 0 :(得分:0)

我认为您的代码没有任何明显错误。

嗯,你显然是在击中你最后一个嵌套的else,这意味着由于某种原因,这个语句$cek->num_rows() > 0正在评估为false。

我假设您输入的是正确且现有的用户名+密码组合。但要解决问题,您可以在$p = md5($psw);之后执行:

echo 'hashed password: ' . $p . '<br>username: ' . $u; exit;

并查看用户名和散列密码是否与数据库中的内容匹配,如果没有,则得到答案。我最好的猜测是你可能没有存储哈希密码而只存储纯文本密码。

作为旁注,您应该将所有重定向和flashdata移出模型。模型应该只返回或抛出例外。