Welcome Guest, Not a member yet? Register   Sign In
Simple Captcha with No Database - Codeigniter
#1

[eluser]Unknown[/eluser]
Very easy to install. Follow the below instructions

1. Google and download the font monofont.ttf

2.Copy the font monofont.ttf to the root folder

3. Copy captcha.php and welcome.php to the controllers folder

4. Copy welcome_message.php to the views folder


controllers/captcha.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Captcha extends CI_Controller {
public function index()
{
  //$this->load->helper('url');
  $this->load->library('session');
  $width = isset($_GET['width']) ? $_GET['width'] : '120';
  $height = isset($_GET['height']) ? $_GET['height'] : '40';
  $this->CaptchaSecurityImages($width,$height);
}
public function CaptchaSecurityImages($width='120',$height='40')
{
  $this->load->library('session');
  $font = 'monofont.ttf';
  /* font size will be 75% of the image height */
  $font_size = $height * 0.75;
  $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  /* set the colours */
  $background_color = imagecolorallocate($image, 255, 255, 255);
  $text_color = imagecolorallocate($image, 20, 40, 100);
  $noise_color = imagecolorallocate($image, 100, 120, 180);
  /* generate random dots in background */
  for( $i=0; $i<($width*$height)/3; $i++ )
  {
   imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  }
  /* generate random lines in background */
  for( $i=0; $i<($width*$height)/150; $i++ )
  {
   imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  }
  /* create textbox and add text */
  $textbox = imagettfbbox($font_size, 0, $font, $this->session->userdata('security_code')) or die('Error in imagettfbbox function');
  $x = ($width - $textbox[4])/2;
  $y = ($height - $textbox[5])/2;
  imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $this->session->userdata('security_code')) or die('Error in imagettftext function');
  /* output captcha image to browser */
  header('Content-Type: image/jpeg');
  imagejpeg($image);
  imagedestroy($image);
}
}


controllers/welcome.php
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
  $this->load->library('session');
  //$this->load->helper('url');  
  // I HAVE LOADED HELPER URL BY AUTOLOADING IT in the file
  //- config/autoload.php using the following code: $autoload['helper'] =  array('url');)
   $data['msg'] ='';
  if(isset($_POST['submit']))
  {
   $data['msg'] = $this->checkCaptcha($_POST);
  }
  $code = $this->generateCode(6);
  $newdata = array('security_code'=> $code);
  $this->session->set_userdata($newdata);
  $this->load->view('welcome_message',$data);
}
public function generateCode($characters)
{
  /* list all possible characters, similar looking characters and vowels have been removed */
  $possible = '23456789bcdfghjkmnpqrstvwxyz';
  $code = '';
  $i = 0;
  while ($i < $characters)
  {
   $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
   $i++;
  }
  return $code;
}
public function checkCaptcha($post)
{
  if($post['captcha'] = $post['ccode'])
  {
   return 'Success';
  }
  else
  {
   return 'Mismatch, try again';
  }
}
}


views/welcome_message.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;
   My Form
  &lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
   &lt;!-- CSS --&gt;
&lt;/head&gt;
&lt;body&gt;
<p>&lt;?php echo $msg; ?&gt;</p>
  &lt;form id="myform" method="post" action=""&gt;
   <p>
    Help us prevent spam:
   </p>
   <div>
    <img src="captcha/" alt="captcha" />
    &lt;input name="ccode" id="ccode" type="hidden" value="&lt;?php echo $this-&gt;session-&gt;userdata('security_code'); ?&gt;" /&gt;
   </div>
   <p>
    Please re-enter the code above:
   </p>
   <div>
    &lt;input name="captcha" id="captcha" type="text" /&gt;
   </div>
   <div>
    &lt;input id="submit" name="submit" value="Submit" type="submit" /&gt;
   </div>
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

That's it! Done Smile




Theme © iAndrew 2016 - Forum software by © MyBB