0
votes

i try to login by using this simple code but after i click the login button the else choice "Invalid Username or Password!" always appear even the username and password is match

Controller :

class Auth100 extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->model('AuthModel100');
    }

    public function login(){
        if($this->input->post('login') && $this->validation('login')){
            $login=$this->AuthModel100->getuser($this->input->post('username'));
            if($login !=NULL){
                if(password_verify($this->input->post('password'), $login->password_100)){
                }
            }
            $this->session->set_flashdata('msg','Invalid Username or Password!');
        }
        $this->load->view('auth100/form_login_100');
    }

    public function logout(){
        redirect('auth100/login');
    }

    public function changepass(){
    if($this->input->post('change') && $this->validation('change')){
            redirect('welcome');
        }   
        $this->load->view('auth100/form_password_100');
    }

    public function validation($type){
        $this->load->library('form_validation');    

        if($type=='login'){
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
        } else {
            $this->form_validation->set_rules('oldpassword', 'Old Password', 'required');
            $this->form_validation->set_rules('newpassword', 'New Password', 'required');
        }

        if($this->form_validation->run())
            return TRUE;
        else 
            return FALSE;
    }

Model :

class AuthModel100 extends CI_Model{

    public function getuser($username){
        $this->db->where('username_100', $username);
        return $this->db->get('users100')->row();
    }
}

View :

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <title>Catshop100 | Login</title>
</head>
<body>
    <div class="container" style="width:50%;">
    <h3>Kitty Fan Shop - Login</h3>
    <hr>
    <div style="color: red;"><?=validation_errors()?></div>
    <form action="" class="form-horizontal" method="post">
        <div class="form-group">
            <label for="username" class="control-label col-sm-2">Username</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="username">
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="control-label col-sm-2">Password</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" name="password">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" name="login" value="Login" class="btn btn-default">
            </div>
        </div>
    </form>
    </div>
</body>
</html>

Database : enter image description here

enter image description here

please help me to fix this

2

2 Answers

0
votes

You're getting an error because the error code is not in the else condition, I've written the corrected code below with necessary comments. See if it helps you.

public function login(){

    if($this->input->post('login') && $this->validation('login')){

        $login=$this->AuthModel100->getuser($this->input->post('username'));

        if($login !=NULL){

            if(password_verify($this->input->post('password'), $login->password_100)){ // success
                // do something -- redirect to dashboard -- your logic

            }else{ // fail

                $this->session->set_flashdata('msg','Invalid Username or Password!');

                $this->load->view('auth100/form_login_100');
            }
        }else{

            // do something if $login is NULL

        }
    }
}  
0
votes

Try this once, you need to handle the else condition so that method can return you the what exactly has happened.

 public function login() {
    if ($this->input->post('login') && $this->validation('login')) {
        $login = $this->AuthModel100->getuser($this->input->post('username'));
        if ($login != NULL) {
            if (password_verify($this->input->post('password'), $login->password_100)) {
                $this->session->set_flashdata('msg', 'Login Successful!');
            } else {
                $this->session->set_flashdata('msg', 'You have entered an invalid password');
            }
        } else {
            $this->session->set_flashdata('msg', 'You have not entered username');
        }
    }
    $this->load->view('auth100/form_login_100');
}