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

[eluser]Glowball[/eluser]
Hi all, I have a site with a form on every page, and that form needs a captcha. The code for the form is in a single view template used by all pages, so it is only in one place. I'd rather not put the captcha-building code in a view, but I'm not sure where else to put it.

Some of my controllers have multiple functions, so it seems like it would be best to put the code in the constructor for each controller. But then it seems that I can't return the captcha image code from the constructor to pass it to the view.

Do I need to put the code in each function? It seems so redundant. What would you do?
#2

[eluser]Mirage[/eluser]
I would write a view helper function that generates the captcha, and then call that from the view.
#3

[eluser]Glowball[/eluser]
[quote author="Mirage" date="1222901407"]I would write a view helper function that generates the captcha, and then call that from the view.[/quote]
Thanks, that should work, good idea!
#4

[eluser]Glowball[/eluser]
Great solution! I made a little helper at application/helpers/captcha_helper.php with this in it, including it in each file by putting it in my autoload.php file.
Code:
if (! function_exists('get_captcha'))
{
    function get_captcha()
    {
        $CI =& get_instance();
        
        $captchaValues = array(
            'img_path'     => './captcha/',
            'img_url'     => 'http://SITENAME.com/captcha/'
            );
        
        $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'];
    }
}

Then in my view I just called it like so.

Code:
<?= get_captcha(); ?>

Thanks so much for your quick help!
#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




Theme © iAndrew 2016 - Forum software by © MyBB