Welcome Guest, Not a member yet? Register   Sign In
Math CAPTCHA library

[eluser]Dan Murfitt[/eluser]

I've just released an early version of a math CAPTCHA library. It needs proper testing (which I intend to do over the next week). If anyone wants to have a go, or offer some suggestions, it would be much appreciated.

The library repository and latest version can be found at: https://github.com/danmurf/CI-MathCaptcha

Latest version download: https://github.com/danmurf/CI-MathCaptch...all/master

Here is some of the information I've posted to GitHub:

A simple math based CAPTCHA library for CodeIgniter with various configurable options.

Supports addition and multiplication
Multi-language support
Questions written in plain English (or your chosen language) in various phrases
Questions can contain numbers as words, numeric or random
Answers can be enforced to numeric, words or either
Add your own question phrases for added randomness

Copy the application/libraries/mathcaptcha_library.php file to application/libraries/
Copy the application/language/english/mathcaptcha_lang.php to application/language/english/
If you would like to use another language other than English you will need to duplicate the language file and add translations the numbers and phrases respectively
Initialise the math CAPTCHA library and include it in your controller. Example below:

public function myform()

        $data['math_captcha_question'] = $this->mathcaptcha->get_question();

        $this->form_validation->set_rules('math_captcha', 'Math CAPTCHA', 'required|callback__check_math_captcha');

        if ($this->form_validation->run() == FALSE)
            $this->load->view('myform', $data);
            $this->load->view('myform', $data);

Add a callback for the math CAPTCHA form validation if you are using CodeIgniter Form Validation library. Example below:

function _check_math_captcha($str)
    if ($this->mathcaptcha->check_answer($str))
        return TRUE;
        $this->form_validation->set_message('_check_math_captcha', 'Enter a valid math captcha response.');
        return FALSE;

Print the $mathcaptcha_question on your form somewhere. Example below:

<?php echo validation_errors(); ?>

<?php echo form_open(); ?>

    <?php echo $math_captcha_question;?>

    <?php echo form_input('math_captcha');?>

    <?php echo form_submit('submit', 'Submit'); ?>

<?php echo form_close();?>

There are some configuration options which you can pass to the library in an associative array when you $this->mathcaptcha->init($config):

language: This should be set to the language that you want to use. It will default to the language set in the Codeigniter config.php.
operation: The type of math question to use; addition or multiplication. This will default to addition if not specified.
question_format: The type of number to include in the question; numeric, word, or random. This will default to word if not specified.
question_max_number_size: The maximum number size to use in the question. The default is 10, which is also the maximum allowed given the limitations of the language file.
answer_format: The type of answer that is allowed; word means the user must answer in a word, numeric means the user must enter the number or either for, well, either.

Hope it helps Smile

Many thanks

Theme © iAndrew 2016 - Forum software by © MyBB