FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Captcha Security





imagefree
This is file name CaptchaSecurityImages.php. <<<The name speaks what this file do.
Used as
Code:
<img src="CaptchaSecurityImages.php" />

Heres the source
My version:
Code:
<?php
session_start();
/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
class CaptchaSecurityImages {
   
   function font_name()
   {
      $font_dir = "fonts/";
      $open_dir = opendir($font_dir);
      while ($font_name = readdir($open_dir))
         if (($font_name != ".") && ($font_name != ".."))
            $fonts[] = $font_name;
      
      closedir($open_dir);
      $random_font=rand(0, count($fonts)-1);
      $font_name = $font_dir.$fonts[$random_font]; //'monofont.ttf';
      return $font_name;
   }
   
   function generateCode($characters)
   {
      $possible_characters = '23456789bcdfghjkmnpqrstvwxyz';
      $security_code = '';
      $i = 0;
      while ($i < $characters) {
         $security_code .= substr($possible_characters, mt_rand(0, strlen($possible_characters)-1), 1);
         $i++;
      }
      return $security_code;
   }

   function CaptchaSecurityImages($width,$height,$characters)
   {
      $font = $this->font_name();
      $code = $this->generateCode($characters);
      
      /* font size will be 75% of the image height */
      if((int) $height * .75 > 18)
         $font_size = rand(18 , (int) $height * 0.75);
      else
         $font_size = 18;
      $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, rand( 0 , 50), rand(0, 50), rand(0,10));
      $noise_color = imagecolorallocate($image, rand( 100 , 150), rand(150, 200), rand(200,250));
      /* 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, $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 , $code); // or die('Error in imagettftext function');
      /* output captcha image to browser */
      header('Content-Type: image/jpeg');
      header ("Content-Disposition: inline; filename=".md5($code));
      imagejpeg($image);
      imagedestroy($image);
      $_SESSION['security_code'] = $code;
   }
}
$width = 150;
$height = 50;
$characters = 4;
$captcha = new CaptchaSecurityImages($width,$height,$characters);
?>


I have modified it a lot according to my needs. I have very little experience of working with images, and its the first time i worked with classes.

I found the original source a little bit insecure (because of use of GET. This use of GET creates problems when a person uses Opera. I dont know the behavious of other browsers. So, when a person opens a page where this captcha image is used, and then the same image is opened with some other GET values, both images are treated as different by the browser but on the server the CODE value stored in the sessions is overwritten. This definitely creates problems in Captcha Code validation.).

So, Guys please comment on the reliability of my efforts.

Thanks.

If you like it, you may use it according to license. But atleast leave a reply.

Download for Testing

Thanks again.
AftershockVibe
Apart from the education you'll get from it, there's really no need to implement your own Captcha.

You might want to check out ReCaptcha which is not only a good captcha system and will work on all browsers without issue, but will help digitise historic books while you're at it.
Related topics
Novell acquires Linux security company
*OFFICIAL* Which Browser do you use?
Bogus Microsoft Security Update Circulates
php nuke - a wesome CMS
Security website
New Netscape sizzles with security
More than 30 killed in London explosions (Al Qaeda?)
MY Security Wall Gone
Security Code Graphic
The Ultimate PC Security Guide!
Google earth-- Big security Hole..White house hidden now
Homeland Security downplays N.Y.C. threat
Help required with Captcha script
Ode to our Kitchen Spammer
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.