CodeIgniter Forums
Simple captcha - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Simple captcha (/showthread.php?tid=27708)



Simple captcha - El Forum - 02-18-2010

[eluser]MaD HamsteR[/eluser]
Description

This is simple captcha validation library, based on sessions, no DB required. It can be used as stand alone class as well, CI isn't necessary.


Requirements

GD
Sessions


Demo & download

http://dc.alfaspace.net/captcha


Note

This captcha uses SESSIONS, so before using it ensure that user has browser cookies on!


Config

You can configure paths, fonts, and backgrounds and more in captcha/captcha.php

CodeIgniter users DON'T FORGET TO CHANGE FONTS FOLDER & BGS PATHS!

You can also confiruge captcha at runtime.

Use GIF for backgrounds.


Example usage in CI

Code:
<?php

class Welcome extends Controller {

    /**
    * Load simple captcha library
    */

    function Welcome()
    {
        parent::Controller();

        $this->load->library('captcha/captcha');
    }


    /**
    * Show form
    */
    
    function index()
    {
        $this->load->view('index');
    }


    /**
    * Use this method to output captcha image.
    * Ex. img src="index.php/welcome/captcha"
    */

    function captcha()
    {
        $this->captcha->generate();
    }


    /**
    * Use this method in form action url, ex. action="index.php/welcome/validate"
    */

    function validate()
    {

        //Validate user input  

        if($this->captcha->validate($this->input->post('captcha')))
        {
            echo 'OK'; //If captcha string is correct

        }else{

            header('Location: /index.php/welcome'); //Otherwise redirect back

        }

    }



}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */


The idea

When we show captcha image on page like
Code:
<img src="index.php/welcome/captcha">
cookie with SESSION ID is stored on user machine. When user submits the form "validate" method compares input string with captcha string stored in session. Simple as that Smile