[eluser]smickiedoo[/eluser]
way cool!
I created a helper for checking captcha as well. The whole process can be boiled down to one if statement in the controller and a function call in the view to show the captcha image. Couldn't be easier.
my code:
autoloaded helper captcha_check_helper.php
Code:
<?php
if (! function_exists('check_captcha'))
{
function check_captcha() {
$CI =& get_instance();
// First, delete old captchas
$expiration = time()-7200; // Two hour limit
$query = "DELETE FROM captcha WHERE captcha_time < ".$expiration;
$CI->db->query($query);
// Then see if a captcha exists:
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
$binds = array($_POST['captcha'], $CI->input->ip_address(), $expiration);
$query = $CI->db->query($sql, $binds);
$row = $query->row();
if ($row->count == 0)
{
return false;
}
return true;
}
}
?>
autoloaded helper captcha_display_helper.php
Code:
<?php
if (! function_exists('display_captcha'))
{
function display_captcha()
{
$CI =& get_instance();
$captchaValues = array(
'word' => '',
'img_path' => 'system/application/assets/images/captcha/',
'img_url' => base_url() . 'system/application/assets/images/captcha/',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200
);
$cap = create_captcha($captchaValues);
$captchaData = array(
'captcha_id' => '',
'captcha_time' => $cap['time'],
'ip_address' => $CI->input->ip_address(),
'word' => $cap['word']
);
$query = $CI->db->insert_string('captcha', $captchaData);
$CI->db->query($query);
return $cap['image'];
}
}
?>
In the controller just do something like this
Code:
if(check_captcha())
{
//some code
}
And in the view
Code:
<?= display_captcha(); ?>
Thanks