
Create a controller
application/controllers/captcha.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php if ( ! defined( 'BASEPATH' )) exit ( 'No direct script access allowed' ); class Captcha extends CI_Controller { public function __construct() { parent:: __construct(); $this ->load->helper( "url" ); $this ->load->helper( 'form' ); $this ->load->helper( 'captcha' ); $this ->load->library( 'form_validation' ); } public function index() { //validating form fields $this ->form_validation->set_rules( 'username' , 'Email Address' , 'required' ); $this ->form_validation->set_rules( 'user_password' , 'Password' , 'required' ); $this ->form_validation->set_rules( 'userCaptcha' , 'Captcha' , 'required|callback_check_captcha' ); $userCaptcha = $this ->input->post( 'userCaptcha' ); if ( $this ->form_validation->run() == false){ // numeric random number for captcha $random_number = substr (number_format(time() * rand(),0, '' , '' ),0,6); // setting up captcha config $vals = array ( 'word' => $random_number , 'img_path' => './captcha_images/' , 'img_url' => base_url(). 'captcha_images/' , 'img_width' => 140, 'img_height' => 32, 'expiration' => 7200 ); $data [ 'captcha' ] = create_captcha( $vals ); $this ->session->set_userdata( 'captchaWord' , $data [ 'captcha' ][ 'word' ]); $this ->load->view( 'captcha' , $data ); } else { // do your stuff here. echo 'I m here clered all validations' ; } } public function check_captcha( $str ){ $word = $this ->session->userdata( 'captchaWord' ); if ( strcmp ( strtoupper ( $str ), strtoupper ( $word )) == 0){ return true; } else { $this ->form_validation->set_message( 'check_captcha' , 'Please enter correct words!' ); return false; } } } |
Create a view
application/views/captcha.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <html> <head> <title>Adding a Captcha!</title> </head> <body> <h1>Captcha Example</h1> <?php echo form_open( 'captcha' ); ?> < div class = "formSignIn" > < div class = "form-group" > <input autocomplete= "off" type= "text" id= "username" name= "username" placeholder= "User Email" value= "<?php if(!empty($username)){ echo $username;} ?>" /> <span class = "required-server" ><?php echo form_error( 'username' , '<p style="color:#F83A18">' , '</p>' ); ?></span> </ div > < div class = "form-group" > <input autocomplete= "off" type= "password" id= "user_password" name= "user_password" placeholder= "User Password" value= "" /> <span class = "required-server" ><?php echo form_error( 'user_password' , '<p style="color:#F83A18">' , '</p>' ); ?></span> </ div > < div class = "form-group" > <label for = "captcha" ><?php echo $captcha[ 'image' ]; ?></label> <br> <input type= "text" autocomplete= "off" name= "userCaptcha" placeholder= "Enter above text" value= "<?php if(!empty($userCaptcha)){ echo $userCaptcha;} ?>" /> <span class = "required-server" ><?php echo form_error( 'userCaptcha' , '<p style="color:#F83A18">' , '</p>' ); ?></span> </ div > < div class = "form-group" > <input type= "submit" class = "btn btn-success" value= "Sign In" name= "" /> </ div > </ div > <?php echo form_close(); ?> </body> </html> |
Download http://bit.ly/2Vgfjph