简体   繁体   English

Codeigniter登录不起作用

[英]Codeigniter login not working

I'm re-using the login system from an old CodeIgniter project I made over a year ago, I'm a little rusty but I've looked for a while and I can't figure out why the username and password won't work. 我正在重复使用一年多以前的旧CodeIgniter项目中的登录系统,我有些生疏,但已经寻找了一段时间,所以我不知道为什么用户名和密码不会工作。 The register code I used encrypts using MD5 and I've set an encryption key up. 我使用的注册码是使用MD5加密的,并且已经设置了加密密钥。

login view code: 登录视图代码:

            <?php echo form_open('Login_controller/Login'); ?>
            <label for="Username">Username:</label>
            <input type="text" id="Username" name="Username" autofocus  placeholder="Your username"/>
            <label for="Password">Password:</label>
            <input type="password" id="Password" name="Password" placeholder="Your password" />

            <input type="submit" name="Login" value="Login" > 
            <?php echo validation_errors(); ?> 
            <p>Not an existing user? <a href="<?php echo site_url("Register_controller")?>">REGISTER</a><p> 
        </form> 

Login controller code: 登录控制器代码:

    function Login()
{
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('Username', 'Username', 'trim|required');
    $this->form_validation->set_rules('Password', 'Password', 'trim|required|callback_CheckDatabase');

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('Login_view'); //Reloads the login view with validation errors if the login attempt is unsuccessful.//
    }

    else
    { 
        redirect('Home_controller'); //Redirect to the homepage on successful login attempt.//
    }
}


function CheckDatabase($password) //This function is only run when password validation is correct.//
{
    $username = $this->input->post('Username'); //Sets the username as a $username.//
    $result = $this->User_model->Login($username, $password);

    if($result)
    {
        $sess_array = array();
        foreach($result as $row)
        {
            $sess_array = array( //Makes an array of the data to be stored in the session.//
            'id' => $row->UserID
            );

            $this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
        }

        return TRUE;
   }

    else //Ran if the username or password aren't matched in the CIUsers database. Returns error message.//
    {
        $this->form_validation->set_message('CheckDatabase', 'Invalid login details.');
        return false;
    }
}

User model code: 用户型号代码:

    function Login($username, $password)
  {
    $this -> db -> select('UserID, Username, Password');
    $this -> db -> from('CIUsers');
    $this -> db -> where('Username', $username);
    $this -> db -> where('Password', md5($password));
    $this -> db -> limit(1);

    $query = $this -> db -> get();

    if($query -> num_rows() == 1)
    {
      return $query->result();
    }

    else
    {
      return false;
    }
 }

It returns my "invalid login details" message and no errors. 它返回我的“无效的登录详细信息”消息,并且没有错误。

I used print 我用印刷

$this->db->last_query(); 

and realised that I was limiting my password length in the database to 20 without realising the encryption made it longer than 20, changing it to 50 fixed the issue. 并意识到我将数据库中的密码长度限制为20,而没有意识到加密使其长度超过20,将其更改为50可以解决此问题。

First thing md5 is not secure for login you should try password hashing techniqe. 首先,md5对于登录不安全,您应该尝试使用密码哈希技术。

try like this 这样尝试

 public function login() {
 // create the data object
 $data = new stdClass();
 // load form helper and validation library
 $this->load->helper('form');
 $this->load->library('form_validation');
 // set validation rules
 $this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
 $this->form_validation->set_rules('password', 'Password', 'required');
 if ($this->form_validation->run() == false) {
 // validation not ok, send validation errors to the view
 $this->load->view('header');
 $this->load->view('user/login/login');
 $this->load->view('footer');
 } else {
 // set variables from the form
 $username = $this->input->post('username');
 $password = $this->input->post('password');
 if ($this->user_model->resolve_user_login($username, $password)) {
 $user_id = $this->user_model->get_user_id_from_username($username);
 $user    = $this->user_model->get_user($user_id);
 echo "logged in success";
 } else {
 // login failed
 $data->error = 'Wrong username or password.';
 // send error to the view
 $this->load->view('login', $data);
 }
 }

and in your model like this 在你的模型中

public function resolve_user_login($username, $password) {
 $this->db->select('password');
 $this->db->from('users');
 $this->db->where('username', $username);
 $hash = $this->db->get()->row('password');
 return $this->verify_password_hash($password, $hash);
 }
 public function get_user_id_from_username($username) {
 $this->db->select('id');
 $this->db->from('users');
 $this->db->where('username', $username);
 return $this->db->get()->row('id');
 }
 public function get_user($user_id) {
$this->db->from('users');
 $this->db->where('id', $user_id);
 return $this->db->get()->row();
}
 private function hash_password($password) {
 return password_hash($password, PASSWORD_BCRYPT);
 }
 private function verify_password_hash($password, $hash) {
 return password_verify($password, $hash);
 }

For complete tutorial read this http://w3code.in/2015/09/create-login-and-registration-with-codeigniter/ 有关完整的教程,请阅读此http://w3code.in/2015/09/create-login-and-registration-with-codeigniter/

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

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