Add Captcha in Codeigniter
Write this code inside a function of Controller
$vals = array(
//'word' => 'Random word',
'img_path' => 'assets/captcha/',
'img_url' => base_url().'assets/captcha/',
'font_path' => 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200,
'word_length' => 6,
'font_size' => 16,
'img_id' => 'Imageid',
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
// White background and border, black text and red grid
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(0, 0, 0),
'grid' => array(255, 40, 40)
)
);
$cap = create_captcha($vals);
$data['cap_image'] = $cap['image'];
//$this->session->unset_userdata('scaptcha');
$this->session->set_userdata('scaptcha',$cap['word']);
$this->load->view('templates/header');
$this->load->view('users/register', $data);
$this->load->view('templates/footer');
Inside View file
<p id="cap_img"><?php echo $cap_image; ?></p>
<p>Can't read the image? click <a href="javascript:void(0);" class="refreshCaptcha">here</a> to refresh.</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- captcha refresh code -->
<script>
$(document).ready(function(){
$('.refreshCaptcha').on('click', function(){
$.ajax({
url : "<?php echo base_url().'users/refresh'; ?>",
method : "post",
success:function(data){
$('#cap_img').html(data);
}
})
});
});
</script>
Inside Controller file
public function refresh(){
// Captcha configuration
$vals = array(
//'word' => 'Random word',
'img_path' => 'assets/captcha/',
'img_url' => base_url().'assets/captcha/',
'font_path' => 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200,
'word_length' => 6,
'font_size' => 16,
'img_id' => 'Imageid',
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
// White background and border, black text and red grid
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(0, 0, 0),
'grid' => array(255, 40, 40)
)
);
$captcha = create_captcha($vals);
// Unset previous captcha and set new captcha word
$this->session->unset_userdata('scaptcha');
$this->session->set_userdata('scaptcha',$captcha['word']);
// Display captcha image
echo $captcha['image'];
$vals = array(
//'word' => 'Random word',
'img_path' => 'assets/captcha/',
'img_url' => base_url().'assets/captcha/',
'font_path' => 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200,
'word_length' => 6,
'font_size' => 16,
'img_id' => 'Imageid',
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
// White background and border, black text and red grid
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(0, 0, 0),
'grid' => array(255, 40, 40)
)
);
$cap = create_captcha($vals);
$data['cap_image'] = $cap['image'];
//$this->session->unset_userdata('scaptcha');
$this->session->set_userdata('scaptcha',$cap['word']);
$this->load->view('templates/header');
$this->load->view('users/register', $data);
$this->load->view('templates/footer');
Inside View file
<p id="cap_img"><?php echo $cap_image; ?></p>
<p>Can't read the image? click <a href="javascript:void(0);" class="refreshCaptcha">here</a> to refresh.</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- captcha refresh code -->
<script>
$(document).ready(function(){
$('.refreshCaptcha').on('click', function(){
$.ajax({
url : "<?php echo base_url().'users/refresh'; ?>",
method : "post",
success:function(data){
$('#cap_img').html(data);
}
})
});
});
</script>
Inside Controller file
public function refresh(){
// Captcha configuration
$vals = array(
//'word' => 'Random word',
'img_path' => 'assets/captcha/',
'img_url' => base_url().'assets/captcha/',
'font_path' => 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200,
'word_length' => 6,
'font_size' => 16,
'img_id' => 'Imageid',
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
// White background and border, black text and red grid
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(0, 0, 0),
'grid' => array(255, 40, 40)
)
);
$captcha = create_captcha($vals);
// Unset previous captcha and set new captcha word
$this->session->unset_userdata('scaptcha');
$this->session->set_userdata('scaptcha',$captcha['word']);
// Display captcha image
echo $captcha['image'];
Comments
Post a Comment