ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Code snippet: Captcha revisited

May 21, 2008 9:29am

Subscribe [2]
  • #1 / May 21, 2008 9:29am

    Maxximus

    55 posts

    I know some of you don’t like Captcha at all, and can be quite annoying for the user, especially with ‘funny’ fonts and autogenerated values.

    Still it can help to prevent spam. Different forms have been thought out, and the calculate version I personally like most. For that reason I combined the standard CI Captcha with a small snippet in the view.

    $operator = array('+', '-');
    $captcha_value = mt_rand(20, 100).$operator[mt_rand(0, count($operator)-1)].mt_rand(1, 20);  
    
    $this->load->plugin('captcha');
    $vals = array(
                    'word'         => $captcha_value.'= ?',
                    'img_path'     => './assets/captcha/',
                    'img_url'     => '/assets/captcha/',
                    'font_path'     => './assets/system/fonts/Arial.ttf',
                    'img_width'     => 180,
                    'img_height' => 33,
    );
    
    $cap = create_captcha($vals);
    
    eval("\$captcha_outcome = $captcha_value;");
    $data = array(
                    'captcha_id'    => '',
                    'captcha_time'    => $cap['time'],
                    'ip_address'    => $this->input->ip_address(),
                    'word'            => $captcha_outcome
                );
    
    $query = $this->db->insert_string('captcha', $data);
    $this->db->query($query);
        
    echo "<label for='captcha'>Calculate sum:</label><span><input type='text' name='captcha' value=''  />".$cap['image']."</span>";

    You can create your own $captcha_vals array, or add operators. Just make sure your users can calculate the outcome without a calculator 😉

  • #2 / May 21, 2008 10:57am

    sikkle

    325 posts

    humm, do some test, and maybe add that to wiki captcha section, that would make sense to let people use it.

    thanks !

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases