Welcome Guest, Not a member yet? Register   Sign In
Where to Put Captcha Code?
#5

[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;
    }
    
}
?&gt;

autoloaded helper captcha_display_helper.php
Code:
&lt;?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'];
    }
    
}
?&gt;

In the controller just do something like this
Code:
if(check_captcha())
{
   //some code
}


And in the view
Code:
&lt;?= display_captcha(); ?&gt;

Thanks


Messages In This Thread
Where to Put Captcha Code? - by El Forum - 10-01-2008, 11:09 AM
Where to Put Captcha Code? - by El Forum - 10-01-2008, 11:50 AM
Where to Put Captcha Code? - by El Forum - 10-01-2008, 11:56 AM
Where to Put Captcha Code? - by El Forum - 10-01-2008, 12:18 PM
Where to Put Captcha Code? - by El Forum - 11-22-2008, 02:59 PM



Theme © iAndrew 2016 - Forum software by © MyBB