Codeigniter-登录的captcha不起作用



我进行了captcha登录,到目前为止它还在运行,但有两个问题,第一个问题是,当你的用户名或密码错误时,页面会变为空白。其次,当用户名、密码和captcha都错误时,页面变成空白

但当你的用户名和密码正确,而captcha错误时,它会调用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";
}
}
}

我想到目前为止是因为控制器的代码,我已经做了一些更改,我试着放另一个类似的软件

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

但不幸的是不起作用

最好先在单独的条件下检查captcha,然后再检查验证,所以如果你一切都好,并且你的post值都好,那么你应该这样做:

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();
}

现在,您已经隔离了这两个检查,并使重新验证变得更容易,但更重要的是,如果最初的验证是错误的,则不需要进行表单验证。

不确定你的代码是如何工作的,但在// 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');
}
}

我建议重写控制器并使用Form Validation,因为它也是代码点火器标准库查看此处的官方文档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;
}
}

相关内容

  • 没有找到相关文章