[eluser]tinawina[/eluser]
Just wondering if anyone has set up the ci_captcha plugin and added in the capability to let someone refresh just the captcha image? I have seen this in a lot of places and have used it my own self when I get a captcha image that is just impossible to work out. Thanks!
[eluser]stauf[/eluser]
Hello,
I debute with CI and I don't speak English well .
I also need a captcha who refresh and here is what i made :
I changed the plugin captcha_pi to make it prettier and correspondent to my needs :
here is he :
application/plugins/captcha_pi.php :
Then i make a controller named captcha :
application/controllers/captcha.php
And it's all :
You need to use database :
Code: 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),
KEY (word)
)
And you can call it like this :
Code: $this->load->plugin('captcha');
$data['test'].='Submit the word you see below:<br />';
$data['test'].='<img src="'.site_url("captcha/").'" id="captcha">';
$data['test'].='<input type="text" name="captcha" value="" />';
$data['test'].='';
Then, on the page that accepts the submission you'll have something like this:
$expiration = time()-600; // 10 mn
$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)
$data['message']="You must submit the word that appears in the image<br>".$data['test'];
else $data['message']="GOOD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
You can of course modify it for your needs
It's all...........
[eluser]stauf[/eluser]
Sorry i can't attach files, here is the controller captcha :
Code: class Captcha extends Controller {
function Captcha()
{
parent::Controller();
$this->vals= array(
'alpha' => '115',
'nb_char' => '5',
'angle' => '30',
'img_width' => '100',
'img_height' => '20',
'font_path' => './system/fonts/Jester.ttf',
'expiration' => 7200
);
$this->expiration = time()-600; // 10 mn
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$this->expiration);
$this->load->plugin('captcha');
}
function index()
{
$cap = create_captcha($this->vals);
$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);
$image=$cap['image'];
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
}
function reload()
{
$cap = create_captcha($this->vals);
$sql = " UPDATE captcha SET word = ? WHERE ip_address = ? AND captcha_time > ?";
$binds = array($cap['word'],$this->input->ip_address(), $this->expiration);
$query = $this->db->query($sql, $binds);
$image=$cap['image'];
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
}
}
[eluser]stauf[/eluser]
And the plugin part 1 : application/plugins/captcha_pi.php
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author Rick Ellis
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://www.codeignitor.com/user_guide/license.html
* @link http://www.codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/*
Instructions:
Load the plugin using:
$this->load->plugin('captcha');
The create_captcha() function returns an associative array with this data:
[array]
(
'image' => image
'time' => TIMESTAMP (in microtime)
'word' => CAPTCHA WORD
)
The "time" is the micro timestamp used as the image name without the file
extension. It will be a number like this: 1139612155.3422
The "word" is the word that appears in the captcha image, which if not
supplied to the function, will be a random string.
ADDING A DATABASE
In order for the captcha function to prevent someone from posting, you will need
to add the information returned from create_captcha() function to your database.
Then, when the data from the form is submitted by the user you will need to verify
that the data exists in the database and has not expired.
Here is a table prototype:
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),
KEY (word)
)
Here is an example of usage with a DB.
On the page where the captcha will be shown you'll have something like this:
$this->load->plugin('captcha');
$data['test'].='Submit the word you see below:<br />';
$data['test'].='<img src="'.site_url("captcha/").'" id="captcha">';
$data['test'].='<input type="text" name="captcha" value="" />';
$data['test'].='';
Then, on the page that accepts the submission you'll have something like this:
$expiration = time()-600; // 10 mn
$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)
$data['message']="You must submit the word that appears in the image<br>".$data['test'];
else $data['message']="GOOD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
*/
/**
|==========================================================
| Create Captcha
|==========================================================
|
*/
function create_captcha($data = '', $font_path = '')
{
$defaults = array('alpha'=>115,'nb_char'=> 8,'angle'=> 20, 'word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
foreach ($defaults as $key => $val)
{
if ( ! is_array($data))
{
if ( ! isset($$key) OR $$key == '')
{
$$key = $val;
}
}
else
{
$$key = ( ! isset($data[$key])) ? $val : $data[$key];
}
}
if ( ! extension_loaded('gd'))
{
return FALSE;
}
list($usec, $sec) = explode(" ", microtime());
$now = ((float)$usec + (float)$sec);
// -----------------------------------
// Do we have a "word" yet?
// -----------------------------------
if ($word == '')
{
$pool = '23456789ABCDEFGHIJKLMNPQRSTUVWXYZ';
$str = '';
for ($i = 0; $i < $nb_char; $i++)
{
$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
$word = $str;
}
// -----------------------------------
// Determine angle and position
// -----------------------------------
$length = strlen($word);
$x_axis = rand(6, (360/$length)-16);
$y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
// -----------------------------------
// Create image
// -----------------------------------
function aleatoire($tab){
$max = count($tab)-1;
$hasard = mt_rand(0,$max);
return ($tab[$hasard]);
}
$im = imagecreatetruecolor($img_width, $img_height);
// -----------------------------------
// Assign colors
// -----------------------------------
$bg_color = imagecolorallocate ($im, 255, 255, 255);
$border_color = imagecolorallocate ($im, 182,182,182);
$colors = array(imagecolorallocate($im, 255,0,0), // rouge
imagecolorallocate($im, 109,30,100), // violet
imagecolorallocate($im, 30,80,180), // bleu
imagecolorallocate($im, 40,100,20), // vert
imagecolorallocate($im, 255,90,0), // orange
imagecolorallocate($im, 130,130,130)); // gris
$colors_alpha = array(imagecolorallocatealpha($im, 255,0,0,$alpha), // rouge
imagecolorallocatealpha($im, 109,30,100,$alpha), // violet
imagecolorallocatealpha($im, 30,80,180,$alpha), // bleu
imagecolorallocatealpha($im, 40,100,20,$alpha), // vert
imagecolorallocatealpha($im, 255,90,0,$alpha), // orange
imagecolorallocatealpha($im, 130,130,130,$alpha)); // gris
[eluser]stauf[/eluser]
part 2 :
Code: // -----------------------------------
// Create the rectangle
// -----------------------------------
ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
// -----------------------------------
// Create the spiral pattern
// -----------------------------------
$theta = 1;
$thetac = 7;
$radius = 16;
$circles = 20;
$points = 32;
for ($i = 0; $i < ($circles * $points) - 1; $i++)
{
$theta = $theta + $thetac;
$rad = $radius * ($i / $points );
$x = ($rad * cos($theta)) + $x_axis;
$y = ($rad * sin($theta)) + $y_axis;
$theta = $theta + $thetac;
$rad1 = $radius * (($i + 1) / $points);
$x1 = ($rad1 * cos($theta)) + $x_axis;
$y1 = ($rad1 * sin($theta )) + $y_axis;
imageline($im, $x, $y, $x1, $y1, aleatoire($colors_alpha));
$theta = $theta - $thetac;
}
// -----------------------------------
// Write the text
// -----------------------------------
$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
if ($use_font == FALSE)
{
$font_size = 5;
$x = rand(10, $img_width/($length)-10);
$y = 0;
}
else
{
$font_size = 11;
$x = rand(10, $img_width/($length)-10);
$y = $font_size+2;
}
for ($i = 0; $i < strlen($word); $i++)
{
if ($use_font == FALSE)
{
$y = rand(2 , ($img_height-$font_size)/3);
imagestring($im, $font_size, $x, $y, substr($word, $i, 1), aleatoire($colors));
$x += ($font_size*2.5);
}
else
{
$y = $img_height/2 +5;
imagettftext($im, $font_size, rand(-$angle,$angle), $x, $y, aleatoire($colors), $font_path, substr($word, $i, 1));
$x += $font_size+5;
}
}
// -----------------------------------
// Create the border
// -----------------------------------
imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
// -----------------------------------
// Generate the image
// -----------------------------------
$cat=array('image'=>$im,'word' => $word, 'time' => $now );
return $cat;
}
?>
[eluser]stauf[/eluser]
To finish, if you want to change options, you can make it in the controller captcha.php by changing $vals, I personally used the Jester.ttf font .
|