【问题标题】:Captcha code case sensitivity in CodeIgniterCodeIgniter 中的验证码区分大小写
【发布时间】:2013-08-22 17:16:08
【问题描述】:

我在我的一个应用程序中使用 CI,在这里我实现了 CI 验证码。我使用random_string('alnum', 8) 创建大小写组合验证码。此处用户必须编写与图像完全相同的验证码;这是区分大小写的。在这里我需要添加一些技巧;我想删除大小写敏感性,即用户可以用大写或小写填充验证码,如图所示。

这是我的验证码实现代码 -

$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;
    }
}

【问题讨论】:

    标签: php codeigniter captcha case-sensitive


    【解决方案1】:

    将您的验证功能更新为: 我为此使用了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;
     }
    }
    

    【讨论】:

    • 感谢您的帮助;这仅适用于小写。在这里用户只能填写小写字母。但我需要该用户能够填写大写或小写字母。
    • @follett 这将消除敏感性,因为在比较时它会被转换为小写。
    【解决方案2】:

    您只需像这样更新您的验证功能 -

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

    试试这个希望这在大写和小写中都有效。让我知道发生了什么。

    【讨论】:

      【解决方案3】:

      看看 PHP 的 strcasecmp 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        相关资源
        最近更新 更多