Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* A Math Captcha Library that displays a number captcha similar to the drupal captcha.
*
* INSTRUCTIONS
*
* Place this class into you application/library directory and name it kaptcha.php
*
* create MY_Form_validation.php, add this code and save it in the application/libraries folder.
*
* <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
* class MY_Form_validation extends CI_Form_validation {
*
* public function kaptcha($value) {
* $answer = $this->CI->input->post('kaptcha_answer');
* if($this->CI->kaptcha->validate_kaptcha($value, $answer)) {
* return TRUE;
* }
* return FALSE;
* }
* }
*
*
* Add this to your controller form validation rules
* $this->form_validation->set_rules('kaptcha', 'Math Question', 'required|is_numeric|kaptcha');
*
* You can either add this to your form using a single line <?php $this->kaptcha->display_kaptcha(); ?>
* or call it as an array <?php $kaptcha = $this->kaptcha->create_kaptcha() ;?> which will give you three items
* First Number, Second Number and an encrypted answer which needs to be put in a hidden input value with the
* name and id set to kaptcha_answer.
*
* The CSS is you bit!
*
*
* !IMPORTANT
* Either load this library using the autoload.php or call it from your controller $this->load->library('kaptcha');
* You will need to set an encryption key in your application/config/config.php line 220 $config['encryption_key'] = "";.
* Keys can be generated here http://randomkeygen.com/.
*
*/
class Kaptcha {
var $CI;
function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('encrypt');
}
/*
* This function allows you to call the kaptcha as a block
* and insert it in a form as one line of code.
* <?php $kaptcha = $this->kaptcha->display_kaptcha(); ?>
*/
public function display_kaptcha() {
$kaptcha = $this->create_kaptcha();
$display = '';
$display .= '<span id="kaptcha_inst">This question is for testing whether you are a human visitor and to prevent automated spam submissions.</span>';
$display .= '<label for="kaptcha"><span>*</span> Math question: <span id="kaptcha_question">'. $kaptcha['first'] .' + '. $kaptcha['second']. ' = </span></label>';
$display .= '<input type="text" name="kaptcha" id="kaptcha" maxlength="5" />';
$display .= '<span id="kaptcha_hint">'.$kaptcha['hint'].'</span>';
$display .= '<input type="hidden" name="kaptcha_answer" id="kaptcha_answer" value="'. $kaptcha['answer'].'" /> ';
return $display;
}
*/
* Function that returns two parts of the math question and an encrypted answer
*/
public function create_kaptcha() {
$display = array();
$display['first'] = rand(1,10);
$display['second'] = rand(1,10);
$answer = $display['first'] + $display['second'];
$display['answer'] = $this->CI->encrypt->encode($answer);
$display['hint'] = 'Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.';
return $display;
}
*/
* Function that validates the user answer against the encrypted answer
*/
public function validate_kaptcha($kaptcha = null, $answer = null) {
if($kaptcha && $answer) {
$value = $this->CI->encrypt->decode($answer);
if($kaptcha == $value) {
return TRUE;
}
}
return FALSE;
}
}