CodeIgniter中的Captcha代码区分大小写


Captcha code case sensitivity in CodeIgniter

我在一个应用程序中使用CI,在这里我实现了CI captcha。我使用random_string('alnum', 8)创建大小写组合captcha代码。在这里,用户必须编写captcha代码,该代码在图像中准确显示;这是个案敏感性的。在这里我需要添加一些技巧;我想删除casesensitive,即用户可以用大写或小写填充captcha代码,如图所示。

这是我的captcha实现的代码-

$cap_word = random_string('alnum', 8);
$options = array(
 'word'   => $cap_word,
 'img_path'  => './captcha/',
 'img_url'   => base_url() . 'captcha/',
 'font_path'    => './fonts/custom.ttf',
 'img_width'    => 150,
 'img_height' => 30,
 'expiration' => 7200
 );
$cap = create_captcha($options);

这是我检查的回调函数-

public function validate_captcha_code() {
    $cap = $this->input->post('captcha_code');
    if($cap != $cap_word) {
    $this->form_validation->set_message('validate_captcha_code', 'Wrong captcha code, Please try again.');
        return false;
    }else{
        return true;
    }
}

您只需像那样更新验证函数-

   public function validate_captcha_code() {
    $cap = $this->input->post('captcha_code');
        if(strtolower($cap) != strtolower($cap_word) || strtoupper($cap) != strtoupper($cap_word)) {
            $this->form_validation->set_message('validate_captcha_code', 'Wrong captcha code, Please try again.');
            return false;
        }else{
            return true;
        }
}

试试这个,希望它能同时使用大写和小写。让我知道发生了什么。

将验证函数更新为:为此,我使用了strtolower()。

public function validate_captcha_code() {
$cap = strtolower($this->input->post('captcha_code'));
if($cap != strtolower($cap_word)) {
$this->form_validation->set_message('validate_captcha_code', 'Wrong captcha code, Please try again.');
    return false;
}else{
    return true;
 }
}

看看PHP的strcasecmp函数。