Dec
11

Create Simple Captcha For your Web Site

By udamadu  //  PHP, Security, Web Development  //  2 Comments

Brief Description of the Word Captcha

Obviously, CAPTCHA is an acronym. The question is what does it stand for? The answer is fairly simple, as is CAPTCHA’s purpose. A CAPTCHA, or Completely Automated Public Turing-test to tell Computers and Humans Apart, is used to keep bots and other automated programs from signing up for offers, collecting or signing up for email addresses, violating privacy, trying to crack passwords, or sending out spam to unsuspecting email recipients. A CAPTCHA works by issuing a challenge to the person or entity attempting to gain access.

A CAPTCHA challenge is usually a simple visual test or puzzle that a sighted human can complete without much difficulty, but that an automated program cannot understand. The test usually consists of letters, numbers or other images that overlap or intersect. The images are distorted in some way or shown against an intricate background to keep them from being easily read by another computer.

Basic Requirements for Creating Captcha Using PHP

 GD library

GD is an open source code library for the dynamic creation of images by programmers.  You must need to have this Library for creating your captcha image dynamically. You can check your server support GD library functions by checking PHP information.

<?php  phpinfo() ;  ?>

Step By Step for implementing  our Captcha

  • Create a php file for generating our Captcha image. ( I created the file called captcha.php) and add the Session Start Function on top of the file.
    <?php  session_start();  ?>
  • Add the Below Function for generating Random alphanumeric  string of a specific length.//generate a random alphanumeric string of a specific length

function gen_random_string($length) {

$characters = “0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ”.

“abcdefghijklmnopqrstuvwxyz”;

$real_string_length = strlen($characters) – 1;

for($p=0;$p<$length;$p++)

{ $string .= $characters[mt_rand(0, $real_string_length)]; }

return $string;
}

  • add to session array so other scripts can access it for the comparison.
    $_SESSION['image_text']= gen_random_string(8); // 8 random characters chose as our captcha text.
  • Setup the captcha image with a background image.
    $NewImage =imagecreatefrompng(“captcha_background.png”);“imagecreatefrompng” is a GD Library function for creating  a dynamic .PNG  images. Visit http://php.net/manual/en/function.imagecreatefrompng.php to find more info.
  • Now we can insert our random texts on our png image created.$text_array = str_split($_SESSION['image_text']);
    $cntr = 0;

foreach($text_array as $letter){

//generate a random color for each letter

$r = rand(0,200);

$g = rand(0,200);

$b = rand(0,200);

$textcolor = imagecolorallocate($NewImage, $r, $g, $b);

//random horizontal spacing

$spacing = 2*(rand(5,10)+($cntr*10));

//add the character to the image

imagestring($NewImage, 100, $spacing, rand(0,10), $letter, $textcolor);

$cntr++;

}
in above code “imagecolorallocate” is a GD Library function which  sets the color values for our each text. Please Visit http://www.php.net/manual/en/function.imagecolorallocate.php for more info.
the function “imagestring” is another GD Library function. This helps to draw a text horizontally on our png image. To Find more info please visit http://www.php.net/manual/en/function.imagestring.php

  • Now we can Genarate our image as follows.//output the headers than the image as a PNG

header(‘Content-type: image/png’);

header(‘Cache-Control: max-age=0′);

header(‘Expires: ‘.gmdate(‘r’,time()-3600*24*365));

header(‘Pragma:’);

ImagePNG($NewImage);

imagedestroy($NewImage);

Calling our Captcha image

Now we can call our captcha image simply as follows in another php file. (I created the file called index.php)
<img id=’captcha’ name=’captcha’ src=”captcha.php”>

Demo
Please visit  http://demos.mclanka.com/captcha/ to see the demo.

Download Source Code
You can download the source code by visiting http://demos.mclanka.com/captcha/captcha.zip

What is Next
In this tutorial. I was trying to explain how to create our own captcha for our website. As a next step, I ll explain how to apply our captcha for practical examples like form validation etc.

  • web designer

    Great post dude,

  • http://www.wish111.com/ Matthew Crosby

    Great information thank you for sharing how to create capthca for websites

C-panel web hosting
Fotolia
Template Monster
MailChimp

Get Premium Tutorials Free





Related Stuff