Welcome Guest, Not a member yet? Register   Sign In
Simple Captcha Library without creating image files
#1

[eluser]Unknown[/eluser]
Simple captcha library for codeigniter without creating images in the filesystem

1. Load simple_captcha library

2. Create a public function in your controller to get the captcha image & pass a string param to identify it.

Code:
public function get_captcha() {
    print_r($this->simple_captcha->draw_captcha('signup'));
  }

3. Show the image in your form using your controller function

Code:
<img src='signup/get_captcha'/>

4. Get the input value on the form text box & get captcha value in the session using the library function

Code:
$captcha_text = $this->simple_captcha->get_captcha_text('signup');

5. Check weather input text & captcha text matches. if yes the success Smile

simple_captcha.php (library)

Code:
&lt;?php

/**
* Simple Captcha Library
*
* Simple captcha library for codeigniter without creating images in the filesystem
*
* @author   : Dino (DBK)
* @link     : http://technew.in
*
*/

class Simple_captcha {

  private $captcha_text = NULL;

  function __construct() {
    @session_start();
    $this->captcha_text = $this->random_string();
  }

  function set_captcha_session($captcha_area = '') {
    $session_name = 'captcha_' . $captcha_area;
    $_SESSION[$session_name] = $this->captcha_text;
  }

  function get_captcha_text($captcha_area = '') {
    $session_name = 'captcha_' . $captcha_area;
    if (isset($_SESSION[$session_name]))
      $this->captcha_text = $_SESSION[$session_name];
    else
      $this->captcha_text = NULL;
    unset($_SESSION[$session_name]);
    return $this->captcha_text;
  }

  function draw_captcha($captcha_area = '') {
    header("Content-type: image/png");
    $this->set_captcha_session($captcha_area);
    $text_arr = str_split($this->captcha_text, 1);
    $im = @imagecreate('150', '50');
    imageline($im, rand(0, 179), rand(0, 49), rand(0, 179), rand(0, 49), imagecolorallocate($im, rand(200, 255), rand(200, 255), rand(200, 255)));
    imageline($im, rand(0, 179), rand(0, 49), rand(0, 179), rand(0, 49), imagecolorallocate($im, rand(200, 255), rand(200, 255), rand(200, 255)));
    imageline($im, rand(0, 179), rand(0, 49), rand(0, 179), rand(0, 49), imagecolorallocate($im, rand(200, 255), rand(200, 255), rand(200, 255)));
    imageline($im, rand(0, 179), rand(0, 49), rand(0, 179), rand(0, 49), imagecolorallocate($im, rand(200, 255), rand(200, 255), rand(200, 255)));
    imageline($im, rand(0, 179), rand(0, 49), rand(0, 179), rand(0, 49), imagecolorallocate($im, rand(200, 255), rand(200, 255), rand(200, 255)));
    imagestring($im, 5, rand(30, 40), rand(11, 33), $text_arr[0], imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)));
    imagestring($im, 5, rand(50, 60), rand(11, 33), $text_arr[1], imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)));
    imagestring($im, 5, rand(70, 80), rand(11, 33), $text_arr[2], imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)));
    imagestring($im, 5, rand(90, 100), rand(11, 33), $text_arr[3], imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)));
    imagestring($im, 5, rand(110, 120), rand(11, 33), $text_arr[4], imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)));
    imagepng($im);
    imagedestroy($im);
  }

  function random_string() {
    $string = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    return substr(str_shuffle($string), 0, 5);
  }

}

?&gt;

signup.php (sample controller )

Code:
&lt;?php

class Signup extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->library('simple_captcha');
  }

  public function index() {
    if (!$this->input->post()) {
      echo "&lt;form method='post' acti&gt;";
      echo "<img src='signup/get_captcha'/><br/><br/>";
      echo "&lt;input type='text' name='captcha_input' value=''/&gt;";
      echo "&lt;input type='submit' value='Submit'/&gt;";
      echo "&lt;/form&gt;";
    } else {
      $captcha_input = $this->input->post('captcha_input', TRUE);
      $captcha_text = $this->simple_captcha->get_captcha_text('signup');
      if ($captcha_input == $captcha_text) {
        echo 'success';
      } else {
        echo 'wrong captcha';
      }
    }
  }

  public function get_captcha() {
    print_r($this->simple_captcha->draw_captcha('signup'));
  }

}

?&gt;

or

Download Attachment
#2

[eluser]quickshiftin[/eluser]
Thanks for sharing!




Theme © iAndrew 2016 - Forum software by © MyBB