CAPTCHA (Complete Automated Public Turning test to tell Computer and Human Apart) adalah jenis tes security untuk mengidentifikasi apakah pengguna adalah mesin atau manusia. Captcha sendiri banyak digunakan dalam pengembangan aplikasi web untuk melindungi website dari spam. Dalam tutorial ini, kita akan belajar bagaimana menerapkan captcha di CodeIgniter. CodeIgniter menyediakan captcha pembantu untuk membuat CAPTCHA.
berikut kode yang digunakan untuk membuat captcha di CodeIgniter.
// Captcha configuration $config = array( 'img_path' => 'captcha_images/', 'img_url' => base_url().'captcha_images/', 'img_width' => '150', 'img_height' => 50, 'word_length' => 8, 'font_size' => 16 ); $captcha = create_captcha($config);
Anda dapat menggunakan font yang berbeda untuk kata captcha dengan menentukan font_path nilai dalam baris kode konfigurasi captcha.
'font_path' => './path/to/fonts/texb.ttf'
Berikut ini adalah struktur file dan folder aplikasi.
Captcha.php
)Untuk menggunakan CodeIgniter captcha helper, Anda perlu memuat captcha helper yang disertakan dalam fungsi in __construct()
. Fungsi index()
untuk membuat captcha dan menangani proses captcha.
refresh()
function digunakan untuk permintaan kode captcha baru..
('BASEPATH') OR exit('No direct script access allowed');
class Captcha extends CI_Controller
{
function __construct() {
parent::__construct();
// Load the captcha helper
$this->load->helper('captcha');
}
public function index(){
// If captcha form is submitted
if($this->input->post('submit')){
$inputCaptcha = $this->input->post('captcha');
$sessCaptcha = $this->session->userdata('captchaCode');
if($inputCaptcha === $sessCaptcha){
echo 'Captcha code matched.';
}else{
echo 'Captcha code was not match, please try again.';
}
}
// Captcha configuration
$config = array(
'img_path' => 'captcha_images/',
'img_url' => base_url().'captcha_images/',
'img_width' => '150',
'img_height' => 50,
'word_length' => 8,
'font_size' => 16
);
$captcha = create_captcha($config);
// Unset previous captcha and store new captcha word
$this->session->unset_userdata('captchaCode');
$this->session->set_userdata('captchaCode',$captcha['word']);
// Send captcha image to view
$data['captchaImg'] = $captcha['image'];
// Load the view
$this->load->view('captcha/index', $data);
}
public function refresh(){
// Captcha configuration
$config = array(
'img_path' => 'captcha_images/',
'img_url' => base_url().'captcha_images/',
'img_width' => '150',
'img_height' => 50,
'word_length' => 8,
'font_size' => 16
);
$captcha = create_captcha($config);
// Unset previous captcha and store new captcha word
$this->session->unset_userdata('captchaCode');
$this->session->set_userdata('captchaCode',$captcha['word']);
// Display captcha image
echo $captcha['image'];
}
}
captcha/index.php
)DOCTYPE html> <html> <head> <title>Captcha implement in CodeIgniter by CodexWorldtitle> <script src="echo base_url(); ?>assets/js/jquery.min.js">script> <script> $(document).ready(function(){ $('.refreshCaptcha').on('click', function(){ $.get('echo base_url().'captcha/refresh'; ?>', function(data){ $('#captImg').html(data); }); }); }); script> head> <body> <p>Submit the word you see below:p> <p id="captImg">echo $captchaImg; ?>p> <a href="javascript:void(0);" class="refreshCaptcha" ><
img src="echo base_url().'assets/images/refresh.png'; ?>"/>a> <form method="post"> <input type="text" name="captcha" value=""/> <input type="submit" name="submit" value="SUBMIT"/> form> body> html>
LIVE DEMO DOWNLOAD SOURCE CODE
Sekian tutorial kali ini, semoga dapat mambantu pekerjaan anda.