Skip to content

How to use Codeigniter captcha plug in Part 1

Derek Jones edited this page Jul 5, 2012 · 8 revisions

Wiki Categories

Category:Plugin::Tutorials::captcha

Full Article

http://www.okadadesign.no/blog/?p=276

Introduction

I have read a note in Codeigniter captcha_pi.php. You can find this file in system/plugins. However this does not explain well. This post hopefully explains how to use this plugin. And I will show a bit different way in the next post.
I found the following two articles are very helpful and credits should go to them.
http://d.hatena.ne.jp/dix3/20080925 and http://www.blog.valenticabd.com/2008/05/23/captcha-plugin-in-codeigniter.html
The first one is in Japanese. But don't be disappointed with it. You can find the controll code and view code at the end of the blog. These two have English notes as well.
The first step is following what a note in captcha_pi.php is telling.

Environment


I am using XAMPP for my development. I freshly installed a codeigniter1.7.2 in hdocs and renamed as ci_captcha. I have not added any .htaccess or moving application out of system folder.

Preparation


captcha folder


You need to create a folder called captcha. I created it in the same level as system folder. ci_captcha/captcha When you create captcha images, this is the place where all the images are saved.

Database


I used Phpmyadmin to create a database called ci_captcha. Enter this sql.
CREATE TABLE captcha ( captcha_id bigint(13) unsigned NOT NULL auto_increment, captcha_time int(10) unsigned NOT NULL, ip_address varchar(16) default '0' NOT NULL, word varchar(20) NOT NULL, PRIMARY KEY `captcha_id` (`captcha_id`), KEY `word` (`word`) );

And you need to open system/application/config/database.php and modify it.
... $db['default']['hostname'] = "localhost"; $db['default']['username'] = "root1"; $db['default']['password'] = "root"; $db['default']['database'] = "ci_captcha"; $db['default']['dbdriver'] = "mysql"; ...

Autoload.php


Open system/application/config/autoload.php and change the line 44.
$autoload['libraries'] = array('database');

Config.php


Open system/application/config/config.php and change the line 14.
$config['base_url'] = "http://127.0.0.1/ci_captcha/";

Modifying captcha_pi.php

Now open system/plugin/captcha_pi.php

By default, Captcha plugin generates 8 words in length. But if you want to change this length, in line 156 of "captcha_pi.php" there was
$defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
I change to three words for my example.
$defaults = array('word' => '', 'word_length' => 3, 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
Next, in line 226, there was
for ($i = 0; $i < 8; $i++) which I changed to for ($i = 0; $i < $word_length; $i++)
You need to change both line in order to work it properly.

Controller

You create a file htdocs/ci_captcha/system/application/controller/myexample_captcha.php
<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Myexample_captcha extends Controller { function Myexample_captcha() { parent :: Controller(); } function index() { $captcha_result = ''; $data["cap_img"] = $this -> _make_captcha(); if ( $this -> input -> post( 'submit' ) ) { if ( $this -> _check_capthca() ) { $captcha_result = 'GOOD'; }else { $captcha_result = 'BAD'; } } $data["cap_msg"] = $captcha_result; $this -> load -> view( 'testcaptcha_view', $data ); } function _make_captcha() { $this -> load -> plugin( 'captcha' ); $vals = array( 'img_path' => './captcha/', // PATH for captcha ( *Must mkdir (htdocs)/captcha ) 'img_url' => 'http://127.0.0.1/ci_captcha/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_capthca() { // 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($_POST['captcha'], $this->input->ip_address(), $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();

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

}

}//endofclass /**

  • End of file example_captcha.php / /*
  • Location: ./application/controllers/example_captcha.php */

?>

View

Create a file htdocs/ci_captcha/system/application/view/testcapcha_view.php

<html> <head></head> <body> <h2>Captcha Test</h2> <form method="post"> <?php echo $cap_img ;?> <p><?php echo $cap_msg ;?></p> <input type="text" name="captcha" value="" /> <input type="submit" name="submit" value="Submit"> </form> </body> </html>

Test drive

Now visit http://127.0.0.1/ci_captcha/index.php/myexample_captcha to see your captcha. captcha
Clone this wiki locally