简体   繁体   English

Codeigniter-登录验证码不起作用

[英]Codeigniter - captcha for login is not working

I made a captcha login it is working so far but there is two problem and that problem is that first, when your username or password is wrong the page went to blank. 我进行了验证码登录,到目前为止,它仍然可以运行,但是存在两个问题,该问题是,首先,当您的用户名或密码错误时,该页面变为空白。 Second, when both of the username,password and captcha are wrong the page went blank 其次,当用户名,密码和验证码均错误时,页面变为空白

but when your username and password are correct and the captcha is wrong it will call the echo'captcha is not correct'; 但是,当您的用户名和密码正确且验证码错误时,它将调用echo'captcha is not correct';

function aksi_login(){
        $data = array('username' => $this->input->post('username', TRUE),
                        'password' => md5($this->input->post('password', TRUE))
        );


        $this->load->model('m_model'); // load model_user
        $hasil = $this->m_model->cek_user($data);
        if ($hasil->num_rows() == 1 && $this->input->post('submit')){
            $inputCaptcha = $this->input->post('captcha');
            $sessCaptcha = $this->session->userdata('captchaCode');
            if($inputCaptcha === $sessCaptcha){
            foreach ($hasil->result() as $sess) {
                $sess_data['logged_in'] = 'Sudah Login';
                $sess_data['id_user'] = $sess->uid;
                $sess_data['username'] = $sess->username;
                $sess_data['level'] = $sess->level;
                $this->session->set_userdata($sess_data);
            }


            if ($this->session->userdata('level')=='1') {
                redirect('admin');
            }
            elseif ($this->session->userdata('level')=='2') {
                redirect('guru');
            }       
            elseif ($this->session->userdata('level')=='3') {
                redirect('siswa');
            }

        else {
            echo'username or password is wrong'
        }
    }
    else{
                echo "captcha code is not correct";
            }
    }
}

I think so far is because of the controller code and i have made some changes I tried putting another elseif like 我认为到目前为止是由于控制器代码,我进行了一些更改,我尝试放置另一个elseif

elseif ($this->session->userdata('username')== FALSE && $this->session->userdata('password')==FALSE){
echo'username or password is wrong';
}
else {
            echo'username or password is wrong';
    }

but unfortunately is not working 但不幸的是没有用

Its better to check for captcha first in a separate condition then check for validation, so if you got everything right and your post values are all ok then you should do it like this: 最好先在单独的条件下检查验证码,然后再检查验证,所以如果您一切正确,并且帖子值都还可以,那么您应该这样做:

if (this->captcha_validation($this->input->post('captcha')))
{
    $this->form_validation->set_rules($this->rules);
    if ($this->form_validation->run() === TRUE)
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        // your magic
    }
    else
    {
        // array of validation errors
        validation_errors = $this->form_validation->error_array();
    }
}
else
{
    // wrong captcha
    $this->recaptcha();
}

Now you have isolated the two checks and made it way easier for recaptcha, but more importantly you don't need to make form validation if the captcha is wrong in the first place. 现在,您已经隔离了这两项检查,并使其更易于重新捕获,但是更重要的是,如果验证码最初是错误的,则无需进行表单验证。

... ...

Not sure how your code works but in // your magic up there put your logic like this: 不确定代码的工作方式,但是// your magic中放上这样的逻辑:

$this->load->model('m_model'); // load model_user
$hasil = $this->m_model->cek_user($data);
if ($hasil->num_rows() > 0)
{
    foreach ($hasil->result() as $sess) 
    {
        $sess_data['logged_in'] = 'Sudah Login';
        $sess_data['id_user'] = $sess->uid;
        $sess_data['username'] = $sess->username;
        $sess_data['level'] = $sess->level;
        $this->session->set_userdata($sess_data);
    }
    if ($this->session->userdata('level') == '1') 
    {
        redirect('admin');
    }
    elseif ($this->session->userdata('level') == '2') 
    {
        redirect('guru');
    }       
    elseif ($this->session->userdata('level')=='3') 
    {
        redirect('siswa');
    }
}

My suggestion to to rewrite your controller and use Form Validation as it is also codeigniter standard library Check the official documentation here https://www.codeigniter.com/userguide3/libraries/form_validation.html 我建议重写控制器并使用Form Validation,因为它也是codeigniter标准库。在此处查看官方文档https://www.codeigniter.com/userguide3/libraries/form_validation.html

//your validation config should be something like this
public function login() {
  $form_rules = [
            [
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'required',
            ],
            [
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
            ],
            [
                'field' => 'captcha',
                'label' => 'No HP',
                'rules' => 'rules' => 'required|callback_check_captcha'
            ],

  ];

  $this->form_validation->set_rules($this->form_rules);

  if ($this->form_validation->run() == TRUE) {
    //check username & password
    //if you're sure that username is unique, you can directly get 1 data with ->row()
    $check = $this->m_model->cek_user($data)->row();

    if($check) {
       switch($check->level) {
         case '1' :
           break;
         case '2' : 
         ......
         ......
       }
    } else {
        //wrong username / password
    }

  } else {
    //show login form with view
  }

}

/*callback fro form validation*/
public function check_captcha($str) {
if(!empty($this->session->cap_word)) {
    $expiration = time() - $this->config->item('captcha_expiration');
    if($this->session->cap_time > $expiration) {
        if($this->session->cap_word == $str) {
            return TRUE;
        } else {
            $this->form_validation->set_message('check_captcha', 'Wrong captcha.');
            return FALSE;
        }

    } else {
        $this->form_validation->set_message('check_captcha', 'Session captcha expired.');
        return FALSE;
    }

} else {
    $this->form_validation->set_message('check_captcha', 'KWrong captcha.');
    return FALSE;
}
}

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

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