Thursday, June 7, 2012

Codeigniter Captcha Verification using session - An Easy method



Codeigniter Captcha Verification using session - An Easy method


Register function
function register() {
        $this->form_validation->set_rules('txtzip', 'Zipcode', 'trim|required|xss_clean');
$this->form_validation->set_rules('captchafld', 'Submit the word you see below', 'required|callback_captcha_check');

        if ($this->form_validation->run() == FALSE) {

$aCaptchaCfg = array(
//     'word'          => 'myrandomword',    //default: random()
'length' => 6, //default: 5
'img_path' => './captcha/', //no default !
'img_url' => site_url() . 'captcha/', // no default!
'font_path' => site_url() . './system/fonts/', // default: ./system/fonts/
'fonts' => array('texb.ttf', 'TSCu_Comic.ttf'), // default: texb.ttf
'font_size' => 25, // default: 18
'img_width' => '180', // default: 170
'img_height' => '60', // default: 60
'expiration' => 7200 // default: 7200
);

$data['aCaptcha'] = create_captcha($aCaptchaCfg);
$this->session->set_userdata("captchaval",$data['aCaptcha']['word']);

$this->load->view($path.'account_signup', $data);
}
        }
        else {
            // do the action
        }
    }


call back function
function captcha_check($str) {

$captcha_val = $this->session->userdata("captchaval");
       
if ($captcha_val != $str) {
$this->form_validation->set_message('captcha_check', 'Wrong verification number submission');
return FALSE;
} else
return TRUE;
       
    }

Displaying captcha image in the view file



<label>Verification</label><?php
            echo $aCaptcha['image'];
            echo '<input type="text" name="captchafld" value="" />';
            ?>

No comments:

Post a Comment