简体   繁体   中英

Codeigniter Login System Error when match with username and password

i made a login program with codeigniter, there's no problem when i'm entering the wrong username or password in form, it give me failed alert just like in program.at first, i thought there was no problem with acccessing database. and when i'm entering username and password which a already added in database, the fail alert didn't appear just like it should be. but when i'm entering code in if-else program. show alert when fail, and die('success'); if it match password and username. but the die(success) code didnt work, and when i redirect to other page, it also can't work. the program just work if the variabel is FALSE.

this is my views

<?php echo form_open('users/login'); ?>
<div class="row justify-content-md-center" ">

    <div class="col-md-4 col-md-4-offset-4 rounded-lg" style="background-color: white" > 
        <br>
        <h2 class="text-center"><?= $title ?></h2>
        <div class="form-group">
            <input type="text" name="username" class="form-control" placeholder="Masukkan Username" required autofocus>
        </div>
        <div class="form-group">
            <input type="password" name="password" class="form-control" placeholder="Masukkan Password" required autofocus>
        </div>
        <center><button type="submit" class="btn btn-success btn-block">LOGIN</button></center>
        <br>
    </div>


</div>

my controller

<?php
class Users extends CI_Controller{

    }

    public function login(){
        $data['title'] = 'Login Admin ISNU Kalsel';


        $this->form_validation->set_rules('username','Username','required|callback_check_username_exist');
        $this->form_validation->set_rules('password','Password','required');

        if($this->form_validation->run()===FALSE){
            $this->load->view('templates/header');
            $this->load->view('users/login',$data);
            $this->load->view('templates/footer');
        }else{

            $username = $this->input->post('username');
            $password = md5($this->input->post('password'));

            $this->load->model('user_model');
            $user_id = $this->user_model->login($username,$password);

            if($user_id == FALSE){
                die('success');
                $this->session->set_flashdata('login_failed','Login Gagal!');
                redirect('users/login');
            }else{
                $this->session->set_flashdata('user_loggedin','Selamat datang di website ISNU Kalsel');
                redirect('posts');
            }
            //set pesan



        }

    }


}

and my model

<?php
class User_model extends CI_Model{

    public function __construct(){
        $this->load->database();
    }

    public function login($username,$password){
        $this->db->where('username',$username);
        $this->db->where('password',$password);

        $result = $this->db->get('users');

        if($result->num_rows() == 1){
            return $result->row(0)->id;
        }else{
            return FALSE;
        }
    }



}

The form_validation will not work if the method is GET , which is the default. Also, $this->input->post() will give no result if the form method is not POST , so you need to give attribute method='POST'

<?php 
   echo form_open('users/login', array('method'=>"POST"));
?>

If you want to validate the form with GET method you need to take a look here and here .
See if it helps you.

if($user_id == FALSE){
                die('success');
                $this->session->set_flashdata('login_failed','Login Gagal!');
                redirect('users/login');

should be

if($user_id){
                die('success');
                $this->session->set_flashdata('login_failed','Login Gagal!');
                redirect('users/login');

here we are trying to check if $user_id returns true if it returns false else block would be called. no need to compare $user_id to false ($user_id == FALSE).

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