Pergunta

I use CI in one of my application and here I implement CI captcha. I use random_string('alnum', 8) to create upper and lower case combination captcha code. Here user must neede to write captcha code which is exactly shown in the image; which is casesensetive. Here I need to add some trick; I want to remove the casesensivity which is user can able to filled captcha code with uppercase or with lowercase as he shown in the image.

Here is my captcha implemented code -

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

And here is my callback function for checking -

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;
    }
}
Foi útil?

Solução

You just update your validate function like that -

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

Try this hope this works in both upper and lowercase . And let me know what's going on.

Outras dicas

update your validation function to this : I have used strtolower() for this.

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

Take a look at PHP's strcasecmp function.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top