Web Tutorials     [RO]  [EN]  
| HOME | Tutorials | News | SERVICES | Directory | Tools | FORUM | About | SITE MAP | CONTACT | SEARCH |
.....................................................
.....................................................
User happy birthdayToday we celebrate one day of birth.
(dannyb0y)
.....................................................
Login
Register
I forgot my password
.....................................................
Put your ad here
.....................................................
Online
In total there is
11 visitors online,
of which:
11 are bots
.....................................................
Put your ad here
.....................................................
.....................................................
.....................................................
.....................................................
.
Home - Creating a simple captcha system with PHP

<< Woorank - automatically creates the review of your website   -   Levenstein distance, editing algorithm >>
Rate this article(Members only)
1 2 3 4 5
A - A Announced this way the site administrator for any problems observed on this page.  Print this page as PDF  Email  

Creating a simple captcha system with PHP


Publishing date: 18-02-2011 - Copyright © Claudiu Gilcescu-Ceia

Here's a simple method to create a captcha code with Javascript.

The code is quite easy to figure out and works with any TTF font that you might want to use. You can add more or less noise on the image, as you wish. You can also hard-code other colors in the code. Here's the code:

class.funkycaptcha.php

COD:
class FunkyCaptcha {
  
   var $font = 'arial.ttf';
   var $char_list = '23456789abcdefghjkmnprstvwxyz';
    
   // Set character list
   function setCharlist($list) {
        $this->char_list = $list; 
      }
    
   // Set font
   function setFont($fontname) {
        $this->font = $fontname;
   }
    
   // Return a random code of $characters length
   function CaptchaCode($characters) {
      $captcha = substr(str_shuffle($this->char_list), 0, $characters);
      return $captcha;
   }
    
   // Return the captcha image and store the code and the IP session
   function CreateCaptcha($width='120',$height='40',$characters='6', $dotnoise='80', $linenoise='80') {
      $error = "";
      $code = $this->CaptchaCode($characters);
 
      $font_size = $height * 0.60;
      $image = imagecreate($width, $height);
     
      $background_color = imagecolorallocate($image, 255, 255, 255);
      $text_color = imagecolorallocate($image, 0, 0, 0);
      $noise_color = imagecolorallocate($image, 255, 255, 255);
       
      $textbox = imagettfbbox($font_size, 0, $this->font, $code);
      $x = ($width - $textbox[4])/2;
      $y = ($height - $textbox[5])/2;
       
      imagettftext($image, $font_size, 0, $x-10, $y, $text_color, $this->font , $code);
       
      $dotnoise = abs($dotnoise-100);
      $linenoise = abs($linenoise-100);
       
      if ($dotnoise == 0) $dotnoise = 1;
      if ($linenoise == 0) $linenoise = 1;
 
      $dot_ratio = ($dotnoise/100) * ($width*$height);
      $line_ratio = ($linenoise/100) * ($width*$height);
       
      for( $i=0; $i<($width*$height)/$dot_ratio; $i++ ) {
         imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
      }
       
      for( $i=0; $i<($width*$height)/$line_ratio; $i++ ) {
         imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
      }
       
      if ($error != "") {
        echo "$error";
        die();
      }
      $_SESSION['captcha'] = $code;
        if (isset($_SERVER['REMOTE_ADDR'])) {
            $_SESSION['ipcheck'] = $_SERVER['REMOTE_ADDR'];
            $_SESSION['code'] = $code;   
               } else {
            $_SESSION['ipcheck'] = 'notset';
            $_SESSION['code'] = 'notset';
                   }
          
          ob_end_clean();
          header('Content-Type: image/jpeg');
          imagejpeg($image);
          imagedestroy($image);
          exit;
   }
}

To use it, just like in the example above, here's a short example:

Creating the image

COD:
session_start();
require_once($_SERVER['DOCUMENT_ROOT']."/php/class.funkycaptcha.php");
$captcha = new FunkyCaptcha();
 
$captcha->setFont($_SERVER['DOCUMENT_ROOT'].'/demos/funkycaptcha/arial.ttf');
$captcha->CreateCaptcha(150, 30, 5);

 

Using the image in a form

COD:
<?php
   session_start();
?>
 
<img src="captcha.php" />
<br>
<form name="captcha" action="" method="post">
   <input type="text" name="captcha" />
   <input type="submit" name="send" value="send" /> 
</form>
 
<?php
   if (isset($_POST['captcha'])) {
       if ($_POST['captcha'] == $_SESSION['code']) {
           echo "
               Pass
               <pre>
           ";
           print_r($_SESSION);
           echo "
               </pre>
           ";
       } else {
           echo "Fail";
       }
   }
?>

The code in this article is distributed under the MIT license licence

You can visit the author's website at blog.hazardousgaming.info

Publishing date: 18-02-2011 - Copyright © Claudiu Gilcescu-Ceia   
Click here if you want to see other articles by the same author

There are 2 similar articles, click here for list.

There are no comments on this article. Be the first to say your opinion.

Add a comment on this article (members only - login on the site):
Put your ad here