Thursday, 9 February 2017

Use Captcha Code in a form in Codeigniter by using PHP script

Now go to Controller Under your Insert function () make this function 

public function insert()
    {
        $this->load->database();
        $this->load->model('mode');
        $word = $this->session->userdata('captcha');
        $code = $this->input->post('captcha');
     
        if($word == $code)
        {
            if($data['query']= $this->mode->insert())
            {
              echo "<script>alert('Register Successfully..');window.history.go(-1);</script>";
            }
        }
        else
        {
            echo "<script>alert('Please Enter Correct Captcha..');window.history.go(-1);</script>";
        }
     
    }


Now make a Captcha Function in Model


function captcha()
    {
        $this->load->helper('captcha');
        $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z'));
        $original_string = implode("", $original_string);
        $captcha = substr(str_shuffle($original_string), 0, 6);
        $vals = array(
        'word'          => $captcha,
        'img_path'      => './captcha_images/',
        'img_url'       => base_url().'captcha_images/',
        'font_path'     => FCPATH.'fonts/texb.ttf',
        'img_width'     => '150',
        'img_height'    => 30,
        'expiration'    => 7200,
        'word_length'   => 6,
        'font_size'     => 30,
        'img_id'        => 'Imageid',
        'pool'          => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        // White background and border, black text and red grid
                    'colors'  => array(
                    'background' => array(253, 253, 253),
                    'border' => array(51, 153, 255),
                    'text' => array(0, 0, 0),
                    'grid' => array(192, 253, 253)
                )
        );
        $cap = create_captcha($vals);
        $img = $cap['image'];
        if($ses=$this->session->unset_userdata('captcha'))
        {
            $this->session->set_userdata(array('captcha'=>$captcha));
            echo $img;
        }
        else
        {
           $this->session->set_userdata(array('captcha'=>$captcha));
           echo $img;
        }

    } 





Now go to your View and write this code.Put this code at the end of your form.

 <div class="col-md-12">
       <div class="form-group col-sm-8">
          <div class="form-group col-lg-12 col-md-12 col-xs-12">
             <span id="cept" ><?php $this->load->model('mode');echo $this->mode->captcha();?>
              </span>
              <a onclick="captcha()" id="tst" class="btn btn-default btn-sm">
              <span class="fa fa-refresh"></span>
              </a>
           </div>
         <div class="form-group col-lg-12 col-md-12 col-xs-12">
             <input type="text" name="captcha" placeholder="enter captcha char..."/>
          </div>
     </div>
</div>


<script type="text/javascript">
// Ajax post for refresh captcha image.
$(document).ready(function() {
$("#tst").click(function() {
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/Cont/captcha",
success: function(res) {
if (res)
{
jQuery("#cept").html(res);
}
}
});
});
});
</script>



Now make a folder as captcha_images into the source file.





1 comment:

  1. There is also another method of calling the captcha and then validating it in codeigniter (like here: https://www.cloudways.com/blog/captcha-in-codeigniter/ ) This method uses basepath function for the job.

    ReplyDelete