简体   繁体   中英

Login registration fails to login in codeigniter?

I've been trying to connect my database with php codeigniter framework because I'm creating a login form for my project, but I've been having problems form validation callback. even if i'm inputting the correct username password. it still gives me incorrect username/password.

Here's my code for the database.php located at application/config :

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'users';
$db['default']['password'] = 'p455w0rd';
$db['default']['database'] = 'users';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Here is the code for my login controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_login extends CI_controller{

    public function index()
    {
        $this -> login();
    }

    public function login(){

        $this->load->view('login');     
    }

    public function members(){
        $this->load->view('members');
    }


    public function login_validation(){

        $this->load->library('form_validation');
        //$this->form_validation->CI =& $this;

        $this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean|callback_validate_credentials');
        $this->form_validation->set_rules('password', 'Password', 'required|md5|trim');

        if($this->form_validation->run($this) == false){
            $this->load->view('login');
        }
        else{
            redirect('c_login/members');
        }

    }

    public function validate_credentials(){

        $this->load->model('model_users');

        if($this->model_users->can_log_in()){
            return true;
        }
        else{
            $this->form_validation->set_message('validate_credentials', 'Incorrect username/password');
            return false;
        }
    }
}

my model_users:

<?php

class Model_users extends CI_Model {

    public function can_log_in(){

        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('users');

        if($query->num_rows() == 1){
            return true;
        }
        else{
            return false;
        }
    }

}

It would be a really great help if you guys can help me fix this problem. i'm using XAMPP 1.7.3a.

remove md5 from this statement

 $this->form_validation->set_rules('password', 'Password', 'required|md5|trim');

you are converting here password into md5 after that you again convert password in your model

  $this->db->where('password', md5($this->input->post('password')));

i think that's the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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