Captcha dengan CodeIgniter

Di sini contoh menggunakan captcha dengan codeigniter. Kod ini menggunakan contoh kod yang terdapat di Wiki web codeigniter. Satu table, satu fail controller, satu fail model dan satu folder diperlukan.

Hasilnya boleh dilihat di sini >> Demo

1. Bina satu folder ‘captcha’ di direktori utama (satu aras dengan folder ‘system’ dan ‘user_guide’)

-->ci_root
     -->captcha
     -->system
     -->user_guide

2. Bina satu table pangkalan data

CREATE TABLE IF NOT EXISTS `captcha` (
  `captcha_id` bigint(13) unsigned NOT NULL auto_increment,
  `captcha_time` int(10) unsigned NOT NULL,
  `ip_address` varchar(16) NOT NULL default '0',
  `word` varchar(20) NOT NULL,
  PRIMARY KEY  (`captcha_id`),
  KEY `word` (`word`)
)

3. Bina satu fail captcha.php di dalam controller

class Captcha extends Controller {

	function Captcha()
	{
	    parent::Controller();	
	}
        
        function index()
        {
            $data['cap_img'] = $this->mcaptcha->make_captcha();
            $this->load->view('myindex',$data);
        }
        
        function mycaptcha()
        {
            $captcha = $this->input->post('captcha');
            if($this->mcaptcha->check_captcha($captcha)){
               $data['msg'] = "Captcha match!";   
            } else {
               $data['msg'] = "Enter captcha . Please try again!";
            }
	    $captcha_result = '';
	    $data['cap_img'] = $this->mcaptcha->make_captcha();
            $this->load->view('myindex', $data);
        }

}

4. Bina satu fail mcaptcha.php di dalam model

class Mcaptcha extends Model {

	function Mcaptcha()
	{
		parent::Model();	
	}
	
	function make_captcha()
	{
		$this->load->plugin('captcha');
		$vals = array(
			'img_path' => 'captcha/', // PATH for captcha ( *Must mkdir (htdocs)/captcha )
			'img_url' => '../captcha/', // URL for captcha img
			'img_width' => 200, // width
			'img_height' => 60, // height
			// 'font_path'    => '../system/fonts/2.ttf',
			'expiration' => 7200
		); 
		// Create captcha
		$cap = create_captcha($vals); 
		// Write to DB
		if ($cap) {
			$data = array(
				'captcha_id' => '',
				'captcha_time' => $cap['time'],
				'ip_address' => $this->input->ip_address(),
				'word' => $cap['word']
			);
			$query = $this->db->insert_string('captcha', $data );
			$this->db->query($query);
		}else {
			return 'Umm captcha not work' ;
		}
	return $cap['image'];
	}

	function check_captcha($captcha)
	{ 
		// Delete old data ( 2hours)
		$expiration = time()-7200 ;
		$sql = "DELETE FROM captcha WHERE captcha_time < ? ";
		$binds = array($expiration);
		$query = $this->db->query($sql, $binds);
  
		//checking input
		$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
		$binds = array($captcha, $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();

		if ($row->count > 0)
		{
			return true;
		}
		return false;
	}

}

5. Bina fail myindex.php di dalam view

if(isset($msg)) echo ''.$msg.'
'; echo form_open('captcha/mycaptcha'); $captcha = array('name' => 'captcha');
 
echo form_close();

Hasilnya boleh dilihat di sini >> Demo

Selamat Mencuba…

rais.my

Leave a Reply

Your email address will not be published. Required fields are marked *

*